mac80211_if.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define __UNDEF_NO_VERSION__
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/etherdevice.h>
  19. #include <linux/sched.h>
  20. #include <linux/firmware.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/bcma/bcma.h>
  24. #include <net/mac80211.h>
  25. #include <defs.h>
  26. #include "nicpci.h"
  27. #include "phy/phy_int.h"
  28. #include "d11.h"
  29. #include "channel.h"
  30. #include "scb.h"
  31. #include "pub.h"
  32. #include "ucode_loader.h"
  33. #include "mac80211_if.h"
  34. #include "main.h"
  35. #define N_TX_QUEUES 4 /* #tx queues on mac80211<->driver interface */
  36. /* Flags we support */
  37. #define MAC_FILTERS (FIF_PROMISC_IN_BSS | \
  38. FIF_ALLMULTI | \
  39. FIF_FCSFAIL | \
  40. FIF_CONTROL | \
  41. FIF_OTHER_BSS | \
  42. FIF_BCN_PRBRESP_PROMISC | \
  43. FIF_PSPOLL)
  44. #define CHAN2GHZ(channel, freqency, chflags) { \
  45. .band = IEEE80211_BAND_2GHZ, \
  46. .center_freq = (freqency), \
  47. .hw_value = (channel), \
  48. .flags = chflags, \
  49. .max_antenna_gain = 0, \
  50. .max_power = 19, \
  51. }
  52. #define CHAN5GHZ(channel, chflags) { \
  53. .band = IEEE80211_BAND_5GHZ, \
  54. .center_freq = 5000 + 5*(channel), \
  55. .hw_value = (channel), \
  56. .flags = chflags, \
  57. .max_antenna_gain = 0, \
  58. .max_power = 21, \
  59. }
  60. #define RATE(rate100m, _flags) { \
  61. .bitrate = (rate100m), \
  62. .flags = (_flags), \
  63. .hw_value = (rate100m / 5), \
  64. }
  65. struct firmware_hdr {
  66. __le32 offset;
  67. __le32 len;
  68. __le32 idx;
  69. };
  70. static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
  71. "brcm/bcm43xx",
  72. NULL
  73. };
  74. static int n_adapters_found;
  75. MODULE_AUTHOR("Broadcom Corporation");
  76. MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
  77. MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
  78. MODULE_LICENSE("Dual BSD/GPL");
  79. /* recognized BCMA Core IDs */
  80. static struct bcma_device_id brcms_coreid_table[] = {
  81. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 23, BCMA_ANY_CLASS),
  82. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 24, BCMA_ANY_CLASS),
  83. BCMA_CORETABLE_END
  84. };
  85. MODULE_DEVICE_TABLE(bcma, brcms_coreid_table);
  86. #ifdef DEBUG
  87. static int msglevel = 0xdeadbeef;
  88. module_param(msglevel, int, 0);
  89. #endif /* DEBUG */
  90. static struct ieee80211_channel brcms_2ghz_chantable[] = {
  91. CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
  92. CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
  93. CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
  94. CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
  95. CHAN2GHZ(5, 2432, 0),
  96. CHAN2GHZ(6, 2437, 0),
  97. CHAN2GHZ(7, 2442, 0),
  98. CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
  99. CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
  100. CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
  101. CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
  102. CHAN2GHZ(12, 2467,
  103. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
  104. IEEE80211_CHAN_NO_HT40PLUS),
  105. CHAN2GHZ(13, 2472,
  106. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
  107. IEEE80211_CHAN_NO_HT40PLUS),
  108. CHAN2GHZ(14, 2484,
  109. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
  110. IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
  111. };
  112. static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
  113. /* UNII-1 */
  114. CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
  115. CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
  116. CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
  117. CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
  118. /* UNII-2 */
  119. CHAN5GHZ(52,
  120. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  121. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  122. CHAN5GHZ(56,
  123. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  124. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  125. CHAN5GHZ(60,
  126. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  127. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  128. CHAN5GHZ(64,
  129. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  130. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  131. /* MID */
  132. CHAN5GHZ(100,
  133. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  134. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  135. CHAN5GHZ(104,
  136. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  137. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  138. CHAN5GHZ(108,
  139. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  140. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  141. CHAN5GHZ(112,
  142. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  143. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  144. CHAN5GHZ(116,
  145. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  146. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  147. CHAN5GHZ(120,
  148. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  149. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  150. CHAN5GHZ(124,
  151. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  152. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  153. CHAN5GHZ(128,
  154. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  155. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  156. CHAN5GHZ(132,
  157. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  158. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
  159. CHAN5GHZ(136,
  160. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  161. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
  162. CHAN5GHZ(140,
  163. IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
  164. IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS |
  165. IEEE80211_CHAN_NO_HT40MINUS),
  166. /* UNII-3 */
  167. CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
  168. CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
  169. CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
  170. CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
  171. CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
  172. };
  173. /*
  174. * The rate table is used for both 2.4G and 5G rates. The
  175. * latter being a subset as it does not support CCK rates.
  176. */
  177. static struct ieee80211_rate legacy_ratetable[] = {
  178. RATE(10, 0),
  179. RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
  180. RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
  181. RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
  182. RATE(60, 0),
  183. RATE(90, 0),
  184. RATE(120, 0),
  185. RATE(180, 0),
  186. RATE(240, 0),
  187. RATE(360, 0),
  188. RATE(480, 0),
  189. RATE(540, 0),
  190. };
  191. static const struct ieee80211_supported_band brcms_band_2GHz_nphy_template = {
  192. .band = IEEE80211_BAND_2GHZ,
  193. .channels = brcms_2ghz_chantable,
  194. .n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
  195. .bitrates = legacy_ratetable,
  196. .n_bitrates = ARRAY_SIZE(legacy_ratetable),
  197. .ht_cap = {
  198. /* from include/linux/ieee80211.h */
  199. .cap = IEEE80211_HT_CAP_GRN_FLD |
  200. IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40,
  201. .ht_supported = true,
  202. .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
  203. .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
  204. .mcs = {
  205. /* placeholders for now */
  206. .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
  207. .rx_highest = cpu_to_le16(500),
  208. .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
  209. }
  210. };
  211. static const struct ieee80211_supported_band brcms_band_5GHz_nphy_template = {
  212. .band = IEEE80211_BAND_5GHZ,
  213. .channels = brcms_5ghz_nphy_chantable,
  214. .n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
  215. .bitrates = legacy_ratetable + BRCMS_LEGACY_5G_RATE_OFFSET,
  216. .n_bitrates = ARRAY_SIZE(legacy_ratetable) -
  217. BRCMS_LEGACY_5G_RATE_OFFSET,
  218. .ht_cap = {
  219. .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
  220. IEEE80211_HT_CAP_SGI_40,
  221. .ht_supported = true,
  222. .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
  223. .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
  224. .mcs = {
  225. /* placeholders for now */
  226. .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
  227. .rx_highest = cpu_to_le16(500),
  228. .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
  229. }
  230. };
  231. /* flags the given rate in rateset as requested */
  232. static void brcms_set_basic_rate(struct brcm_rateset *rs, u16 rate, bool is_br)
  233. {
  234. u32 i;
  235. for (i = 0; i < rs->count; i++) {
  236. if (rate != (rs->rates[i] & 0x7f))
  237. continue;
  238. if (is_br)
  239. rs->rates[i] |= BRCMS_RATE_FLAG;
  240. else
  241. rs->rates[i] &= BRCMS_RATE_MASK;
  242. return;
  243. }
  244. }
  245. static void brcms_ops_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  246. {
  247. struct brcms_info *wl = hw->priv;
  248. spin_lock_bh(&wl->lock);
  249. if (!wl->pub->up) {
  250. wiphy_err(wl->wiphy, "ops->tx called while down\n");
  251. kfree_skb(skb);
  252. goto done;
  253. }
  254. brcms_c_sendpkt_mac80211(wl->wlc, skb, hw);
  255. done:
  256. spin_unlock_bh(&wl->lock);
  257. }
  258. static int brcms_ops_start(struct ieee80211_hw *hw)
  259. {
  260. struct brcms_info *wl = hw->priv;
  261. bool blocked;
  262. int err;
  263. ieee80211_wake_queues(hw);
  264. spin_lock_bh(&wl->lock);
  265. blocked = brcms_rfkill_set_hw_state(wl);
  266. spin_unlock_bh(&wl->lock);
  267. if (!blocked)
  268. wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
  269. spin_lock_bh(&wl->lock);
  270. /* avoid acknowledging frames before a non-monitor device is added */
  271. wl->mute_tx = true;
  272. if (!wl->pub->up)
  273. err = brcms_up(wl);
  274. else
  275. err = -ENODEV;
  276. spin_unlock_bh(&wl->lock);
  277. if (err != 0)
  278. wiphy_err(hw->wiphy, "%s: brcms_up() returned %d\n", __func__,
  279. err);
  280. return err;
  281. }
  282. static void brcms_ops_stop(struct ieee80211_hw *hw)
  283. {
  284. struct brcms_info *wl = hw->priv;
  285. int status;
  286. ieee80211_stop_queues(hw);
  287. if (wl->wlc == NULL)
  288. return;
  289. spin_lock_bh(&wl->lock);
  290. status = brcms_c_chipmatch(wl->wlc->hw->vendorid,
  291. wl->wlc->hw->deviceid);
  292. spin_unlock_bh(&wl->lock);
  293. if (!status) {
  294. wiphy_err(wl->wiphy,
  295. "wl: brcms_ops_stop: chipmatch failed\n");
  296. return;
  297. }
  298. /* put driver in down state */
  299. spin_lock_bh(&wl->lock);
  300. brcms_down(wl);
  301. spin_unlock_bh(&wl->lock);
  302. }
  303. static int
  304. brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  305. {
  306. struct brcms_info *wl = hw->priv;
  307. /* Just STA for now */
  308. if (vif->type != NL80211_IFTYPE_STATION) {
  309. wiphy_err(hw->wiphy, "%s: Attempt to add type %d, only"
  310. " STA for now\n", __func__, vif->type);
  311. return -EOPNOTSUPP;
  312. }
  313. wl->mute_tx = false;
  314. brcms_c_mute(wl->wlc, false);
  315. return 0;
  316. }
  317. static void
  318. brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  319. {
  320. }
  321. static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
  322. {
  323. struct ieee80211_conf *conf = &hw->conf;
  324. struct brcms_info *wl = hw->priv;
  325. int err = 0;
  326. int new_int;
  327. struct wiphy *wiphy = hw->wiphy;
  328. spin_lock_bh(&wl->lock);
  329. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  330. brcms_c_set_beacon_listen_interval(wl->wlc,
  331. conf->listen_interval);
  332. }
  333. if (changed & IEEE80211_CONF_CHANGE_MONITOR)
  334. wiphy_dbg(wiphy, "%s: change monitor mode: %s\n",
  335. __func__, conf->flags & IEEE80211_CONF_MONITOR ?
  336. "true" : "false");
  337. if (changed & IEEE80211_CONF_CHANGE_PS)
  338. wiphy_err(wiphy, "%s: change power-save mode: %s (implement)\n",
  339. __func__, conf->flags & IEEE80211_CONF_PS ?
  340. "true" : "false");
  341. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  342. err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
  343. if (err < 0) {
  344. wiphy_err(wiphy, "%s: Error setting power_level\n",
  345. __func__);
  346. goto config_out;
  347. }
  348. new_int = brcms_c_get_tx_power(wl->wlc);
  349. if (new_int != conf->power_level)
  350. wiphy_err(wiphy, "%s: Power level req != actual, %d %d"
  351. "\n", __func__, conf->power_level,
  352. new_int);
  353. }
  354. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  355. if (conf->channel_type == NL80211_CHAN_HT20 ||
  356. conf->channel_type == NL80211_CHAN_NO_HT)
  357. err = brcms_c_set_channel(wl->wlc,
  358. conf->channel->hw_value);
  359. else
  360. err = -ENOTSUPP;
  361. }
  362. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  363. err = brcms_c_set_rate_limit(wl->wlc,
  364. conf->short_frame_max_tx_count,
  365. conf->long_frame_max_tx_count);
  366. config_out:
  367. spin_unlock_bh(&wl->lock);
  368. return err;
  369. }
  370. static void
  371. brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
  372. struct ieee80211_vif *vif,
  373. struct ieee80211_bss_conf *info, u32 changed)
  374. {
  375. struct brcms_info *wl = hw->priv;
  376. struct wiphy *wiphy = hw->wiphy;
  377. if (changed & BSS_CHANGED_ASSOC) {
  378. /* association status changed (associated/disassociated)
  379. * also implies a change in the AID.
  380. */
  381. wiphy_err(wiphy, "%s: %s: %sassociated\n", KBUILD_MODNAME,
  382. __func__, info->assoc ? "" : "dis");
  383. spin_lock_bh(&wl->lock);
  384. brcms_c_associate_upd(wl->wlc, info->assoc);
  385. spin_unlock_bh(&wl->lock);
  386. }
  387. if (changed & BSS_CHANGED_ERP_SLOT) {
  388. s8 val;
  389. /* slot timing changed */
  390. if (info->use_short_slot)
  391. val = 1;
  392. else
  393. val = 0;
  394. spin_lock_bh(&wl->lock);
  395. brcms_c_set_shortslot_override(wl->wlc, val);
  396. spin_unlock_bh(&wl->lock);
  397. }
  398. if (changed & BSS_CHANGED_HT) {
  399. /* 802.11n parameters changed */
  400. u16 mode = info->ht_operation_mode;
  401. spin_lock_bh(&wl->lock);
  402. brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
  403. mode & IEEE80211_HT_OP_MODE_PROTECTION);
  404. brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
  405. mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  406. brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
  407. mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
  408. spin_unlock_bh(&wl->lock);
  409. }
  410. if (changed & BSS_CHANGED_BASIC_RATES) {
  411. struct ieee80211_supported_band *bi;
  412. u32 br_mask, i;
  413. u16 rate;
  414. struct brcm_rateset rs;
  415. int error;
  416. /* retrieve the current rates */
  417. spin_lock_bh(&wl->lock);
  418. brcms_c_get_current_rateset(wl->wlc, &rs);
  419. spin_unlock_bh(&wl->lock);
  420. br_mask = info->basic_rates;
  421. bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
  422. for (i = 0; i < bi->n_bitrates; i++) {
  423. /* convert to internal rate value */
  424. rate = (bi->bitrates[i].bitrate << 1) / 10;
  425. /* set/clear basic rate flag */
  426. brcms_set_basic_rate(&rs, rate, br_mask & 1);
  427. br_mask >>= 1;
  428. }
  429. /* update the rate set */
  430. spin_lock_bh(&wl->lock);
  431. error = brcms_c_set_rateset(wl->wlc, &rs);
  432. spin_unlock_bh(&wl->lock);
  433. if (error)
  434. wiphy_err(wiphy, "changing basic rates failed: %d\n",
  435. error);
  436. }
  437. if (changed & BSS_CHANGED_BEACON_INT) {
  438. /* Beacon interval changed */
  439. spin_lock_bh(&wl->lock);
  440. brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
  441. spin_unlock_bh(&wl->lock);
  442. }
  443. if (changed & BSS_CHANGED_BSSID) {
  444. /* BSSID changed, for whatever reason (IBSS and managed mode) */
  445. spin_lock_bh(&wl->lock);
  446. brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
  447. spin_unlock_bh(&wl->lock);
  448. }
  449. if (changed & BSS_CHANGED_BEACON)
  450. /* Beacon data changed, retrieve new beacon (beaconing modes) */
  451. wiphy_err(wiphy, "%s: beacon changed\n", __func__);
  452. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  453. /* Beaconing should be enabled/disabled (beaconing modes) */
  454. wiphy_err(wiphy, "%s: Beacon enabled: %s\n", __func__,
  455. info->enable_beacon ? "true" : "false");
  456. }
  457. if (changed & BSS_CHANGED_CQM) {
  458. /* Connection quality monitor config changed */
  459. wiphy_err(wiphy, "%s: cqm change: threshold %d, hys %d "
  460. " (implement)\n", __func__, info->cqm_rssi_thold,
  461. info->cqm_rssi_hyst);
  462. }
  463. if (changed & BSS_CHANGED_IBSS) {
  464. /* IBSS join status changed */
  465. wiphy_err(wiphy, "%s: IBSS joined: %s (implement)\n", __func__,
  466. info->ibss_joined ? "true" : "false");
  467. }
  468. if (changed & BSS_CHANGED_ARP_FILTER) {
  469. /* Hardware ARP filter address list or state changed */
  470. wiphy_err(wiphy, "%s: arp filtering: enabled %s, count %d"
  471. " (implement)\n", __func__, info->arp_filter_enabled ?
  472. "true" : "false", info->arp_addr_cnt);
  473. }
  474. if (changed & BSS_CHANGED_QOS) {
  475. /*
  476. * QoS for this association was enabled/disabled.
  477. * Note that it is only ever disabled for station mode.
  478. */
  479. wiphy_err(wiphy, "%s: qos enabled: %s (implement)\n", __func__,
  480. info->qos ? "true" : "false");
  481. }
  482. return;
  483. }
  484. static void
  485. brcms_ops_configure_filter(struct ieee80211_hw *hw,
  486. unsigned int changed_flags,
  487. unsigned int *total_flags, u64 multicast)
  488. {
  489. struct brcms_info *wl = hw->priv;
  490. struct wiphy *wiphy = hw->wiphy;
  491. changed_flags &= MAC_FILTERS;
  492. *total_flags &= MAC_FILTERS;
  493. if (changed_flags & FIF_PROMISC_IN_BSS)
  494. wiphy_dbg(wiphy, "FIF_PROMISC_IN_BSS\n");
  495. if (changed_flags & FIF_ALLMULTI)
  496. wiphy_dbg(wiphy, "FIF_ALLMULTI\n");
  497. if (changed_flags & FIF_FCSFAIL)
  498. wiphy_dbg(wiphy, "FIF_FCSFAIL\n");
  499. if (changed_flags & FIF_CONTROL)
  500. wiphy_dbg(wiphy, "FIF_CONTROL\n");
  501. if (changed_flags & FIF_OTHER_BSS)
  502. wiphy_dbg(wiphy, "FIF_OTHER_BSS\n");
  503. if (changed_flags & FIF_PSPOLL)
  504. wiphy_dbg(wiphy, "FIF_PSPOLL\n");
  505. if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
  506. wiphy_dbg(wiphy, "FIF_BCN_PRBRESP_PROMISC\n");
  507. spin_lock_bh(&wl->lock);
  508. brcms_c_mac_promisc(wl->wlc, *total_flags);
  509. spin_unlock_bh(&wl->lock);
  510. return;
  511. }
  512. static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw)
  513. {
  514. struct brcms_info *wl = hw->priv;
  515. spin_lock_bh(&wl->lock);
  516. brcms_c_scan_start(wl->wlc);
  517. spin_unlock_bh(&wl->lock);
  518. return;
  519. }
  520. static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw)
  521. {
  522. struct brcms_info *wl = hw->priv;
  523. spin_lock_bh(&wl->lock);
  524. brcms_c_scan_stop(wl->wlc);
  525. spin_unlock_bh(&wl->lock);
  526. return;
  527. }
  528. static int
  529. brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
  530. const struct ieee80211_tx_queue_params *params)
  531. {
  532. struct brcms_info *wl = hw->priv;
  533. spin_lock_bh(&wl->lock);
  534. brcms_c_wme_setparams(wl->wlc, queue, params, true);
  535. spin_unlock_bh(&wl->lock);
  536. return 0;
  537. }
  538. static int
  539. brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  540. struct ieee80211_sta *sta)
  541. {
  542. struct brcms_info *wl = hw->priv;
  543. struct scb *scb = &wl->wlc->pri_scb;
  544. brcms_c_init_scb(scb);
  545. wl->pub->global_ampdu = &(scb->scb_ampdu);
  546. wl->pub->global_ampdu->scb = scb;
  547. wl->pub->global_ampdu->max_pdu = 16;
  548. /*
  549. * minstrel_ht initiates addBA on our behalf by calling
  550. * ieee80211_start_tx_ba_session()
  551. */
  552. return 0;
  553. }
  554. static int
  555. brcms_ops_ampdu_action(struct ieee80211_hw *hw,
  556. struct ieee80211_vif *vif,
  557. enum ieee80211_ampdu_mlme_action action,
  558. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  559. u8 buf_size)
  560. {
  561. struct brcms_info *wl = hw->priv;
  562. struct scb *scb = &wl->wlc->pri_scb;
  563. int status;
  564. if (WARN_ON(scb->magic != SCB_MAGIC))
  565. return -EIDRM;
  566. switch (action) {
  567. case IEEE80211_AMPDU_RX_START:
  568. break;
  569. case IEEE80211_AMPDU_RX_STOP:
  570. break;
  571. case IEEE80211_AMPDU_TX_START:
  572. spin_lock_bh(&wl->lock);
  573. status = brcms_c_aggregatable(wl->wlc, tid);
  574. spin_unlock_bh(&wl->lock);
  575. if (!status) {
  576. wiphy_err(wl->wiphy, "START: tid %d is not agg\'able\n",
  577. tid);
  578. return -EINVAL;
  579. }
  580. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  581. break;
  582. case IEEE80211_AMPDU_TX_STOP:
  583. spin_lock_bh(&wl->lock);
  584. brcms_c_ampdu_flush(wl->wlc, sta, tid);
  585. spin_unlock_bh(&wl->lock);
  586. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  587. break;
  588. case IEEE80211_AMPDU_TX_OPERATIONAL:
  589. /*
  590. * BA window size from ADDBA response ('buf_size') defines how
  591. * many outstanding MPDUs are allowed for the BA stream by
  592. * recipient and traffic class. 'ampdu_factor' gives maximum
  593. * AMPDU size.
  594. */
  595. spin_lock_bh(&wl->lock);
  596. brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
  597. (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  598. sta->ht_cap.ampdu_factor)) - 1);
  599. spin_unlock_bh(&wl->lock);
  600. /* Power save wakeup */
  601. break;
  602. default:
  603. wiphy_err(wl->wiphy, "%s: Invalid command, ignoring\n",
  604. __func__);
  605. }
  606. return 0;
  607. }
  608. static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
  609. {
  610. struct brcms_info *wl = hw->priv;
  611. bool blocked;
  612. spin_lock_bh(&wl->lock);
  613. blocked = brcms_c_check_radio_disabled(wl->wlc);
  614. spin_unlock_bh(&wl->lock);
  615. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
  616. }
  617. static void brcms_ops_flush(struct ieee80211_hw *hw, bool drop)
  618. {
  619. struct brcms_info *wl = hw->priv;
  620. no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
  621. /* wait for packet queue and dma fifos to run empty */
  622. spin_lock_bh(&wl->lock);
  623. brcms_c_wait_for_tx_completion(wl->wlc, drop);
  624. spin_unlock_bh(&wl->lock);
  625. }
  626. static const struct ieee80211_ops brcms_ops = {
  627. .tx = brcms_ops_tx,
  628. .start = brcms_ops_start,
  629. .stop = brcms_ops_stop,
  630. .add_interface = brcms_ops_add_interface,
  631. .remove_interface = brcms_ops_remove_interface,
  632. .config = brcms_ops_config,
  633. .bss_info_changed = brcms_ops_bss_info_changed,
  634. .configure_filter = brcms_ops_configure_filter,
  635. .sw_scan_start = brcms_ops_sw_scan_start,
  636. .sw_scan_complete = brcms_ops_sw_scan_complete,
  637. .conf_tx = brcms_ops_conf_tx,
  638. .sta_add = brcms_ops_sta_add,
  639. .ampdu_action = brcms_ops_ampdu_action,
  640. .rfkill_poll = brcms_ops_rfkill_poll,
  641. .flush = brcms_ops_flush,
  642. };
  643. /*
  644. * is called in brcms_bcma_probe() context, therefore no locking required.
  645. */
  646. static int brcms_set_hint(struct brcms_info *wl, char *abbrev)
  647. {
  648. return regulatory_hint(wl->pub->ieee_hw->wiphy, abbrev);
  649. }
  650. void brcms_dpc(unsigned long data)
  651. {
  652. struct brcms_info *wl;
  653. wl = (struct brcms_info *) data;
  654. spin_lock_bh(&wl->lock);
  655. /* call the common second level interrupt handler */
  656. if (wl->pub->up) {
  657. if (wl->resched) {
  658. unsigned long flags;
  659. spin_lock_irqsave(&wl->isr_lock, flags);
  660. brcms_c_intrsupd(wl->wlc);
  661. spin_unlock_irqrestore(&wl->isr_lock, flags);
  662. }
  663. wl->resched = brcms_c_dpc(wl->wlc, true);
  664. }
  665. /* brcms_c_dpc() may bring the driver down */
  666. if (!wl->pub->up)
  667. goto done;
  668. /* re-schedule dpc */
  669. if (wl->resched)
  670. tasklet_schedule(&wl->tasklet);
  671. else
  672. /* re-enable interrupts */
  673. brcms_intrson(wl);
  674. done:
  675. spin_unlock_bh(&wl->lock);
  676. }
  677. /*
  678. * Precondition: Since this function is called in brcms_pci_probe() context,
  679. * no locking is required.
  680. */
  681. static int brcms_request_fw(struct brcms_info *wl, struct pci_dev *pdev)
  682. {
  683. int status;
  684. struct device *device = &pdev->dev;
  685. char fw_name[100];
  686. int i;
  687. memset(&wl->fw, 0, sizeof(struct brcms_firmware));
  688. for (i = 0; i < MAX_FW_IMAGES; i++) {
  689. if (brcms_firmwares[i] == NULL)
  690. break;
  691. sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
  692. UCODE_LOADER_API_VER);
  693. status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
  694. if (status) {
  695. wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
  696. KBUILD_MODNAME, fw_name);
  697. return status;
  698. }
  699. sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
  700. UCODE_LOADER_API_VER);
  701. status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
  702. if (status) {
  703. wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
  704. KBUILD_MODNAME, fw_name);
  705. return status;
  706. }
  707. wl->fw.hdr_num_entries[i] =
  708. wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
  709. }
  710. wl->fw.fw_cnt = i;
  711. return brcms_ucode_data_init(wl, &wl->ucode);
  712. }
  713. /*
  714. * Precondition: Since this function is called in brcms_pci_probe() context,
  715. * no locking is required.
  716. */
  717. static void brcms_release_fw(struct brcms_info *wl)
  718. {
  719. int i;
  720. for (i = 0; i < MAX_FW_IMAGES; i++) {
  721. release_firmware(wl->fw.fw_bin[i]);
  722. release_firmware(wl->fw.fw_hdr[i]);
  723. }
  724. }
  725. /**
  726. * This function frees the WL per-device resources.
  727. *
  728. * This function frees resources owned by the WL device pointed to
  729. * by the wl parameter.
  730. *
  731. * precondition: can both be called locked and unlocked
  732. *
  733. */
  734. static void brcms_free(struct brcms_info *wl)
  735. {
  736. struct brcms_timer *t, *next;
  737. /* free ucode data */
  738. if (wl->fw.fw_cnt)
  739. brcms_ucode_data_free(&wl->ucode);
  740. if (wl->irq)
  741. free_irq(wl->irq, wl);
  742. /* kill dpc */
  743. tasklet_kill(&wl->tasklet);
  744. if (wl->pub)
  745. brcms_c_module_unregister(wl->pub, "linux", wl);
  746. /* free common resources */
  747. if (wl->wlc) {
  748. brcms_c_detach(wl->wlc);
  749. wl->wlc = NULL;
  750. wl->pub = NULL;
  751. }
  752. /* virtual interface deletion is deferred so we cannot spinwait */
  753. /* wait for all pending callbacks to complete */
  754. while (atomic_read(&wl->callbacks) > 0)
  755. schedule();
  756. /* free timers */
  757. for (t = wl->timers; t; t = next) {
  758. next = t->next;
  759. #ifdef DEBUG
  760. kfree(t->name);
  761. #endif
  762. kfree(t);
  763. }
  764. }
  765. /*
  766. * called from both kernel as from this kernel module (error flow on attach)
  767. * precondition: perimeter lock is not acquired.
  768. */
  769. static void brcms_remove(struct bcma_device *pdev)
  770. {
  771. struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
  772. struct brcms_info *wl = hw->priv;
  773. if (wl->wlc) {
  774. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
  775. wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
  776. ieee80211_unregister_hw(hw);
  777. }
  778. brcms_free(wl);
  779. bcma_set_drvdata(pdev, NULL);
  780. ieee80211_free_hw(hw);
  781. }
  782. static irqreturn_t brcms_isr(int irq, void *dev_id)
  783. {
  784. struct brcms_info *wl;
  785. bool ours, wantdpc;
  786. wl = (struct brcms_info *) dev_id;
  787. spin_lock(&wl->isr_lock);
  788. /* call common first level interrupt handler */
  789. ours = brcms_c_isr(wl->wlc, &wantdpc);
  790. if (ours) {
  791. /* if more to do... */
  792. if (wantdpc) {
  793. /* ...and call the second level interrupt handler */
  794. /* schedule dpc */
  795. tasklet_schedule(&wl->tasklet);
  796. }
  797. }
  798. spin_unlock(&wl->isr_lock);
  799. return IRQ_RETVAL(ours);
  800. }
  801. /*
  802. * is called in brcms_pci_probe() context, therefore no locking required.
  803. */
  804. static int ieee_hw_rate_init(struct ieee80211_hw *hw)
  805. {
  806. struct brcms_info *wl = hw->priv;
  807. struct brcms_c_info *wlc = wl->wlc;
  808. struct ieee80211_supported_band *band;
  809. int has_5g = 0;
  810. u16 phy_type;
  811. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  812. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  813. phy_type = brcms_c_get_phy_type(wl->wlc, 0);
  814. if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
  815. band = &wlc->bandstate[BAND_2G_INDEX]->band;
  816. *band = brcms_band_2GHz_nphy_template;
  817. if (phy_type == PHY_TYPE_LCN) {
  818. /* Single stream */
  819. band->ht_cap.mcs.rx_mask[1] = 0;
  820. band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
  821. }
  822. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  823. } else {
  824. return -EPERM;
  825. }
  826. /* Assume all bands use the same phy. True for 11n devices. */
  827. if (wl->pub->_nbands > 1) {
  828. has_5g++;
  829. if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
  830. band = &wlc->bandstate[BAND_5G_INDEX]->band;
  831. *band = brcms_band_5GHz_nphy_template;
  832. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  833. } else {
  834. return -EPERM;
  835. }
  836. }
  837. return 0;
  838. }
  839. /*
  840. * is called in brcms_pci_probe() context, therefore no locking required.
  841. */
  842. static int ieee_hw_init(struct ieee80211_hw *hw)
  843. {
  844. hw->flags = IEEE80211_HW_SIGNAL_DBM
  845. /* | IEEE80211_HW_CONNECTION_MONITOR What is this? */
  846. | IEEE80211_HW_REPORTS_TX_ACK_STATUS
  847. | IEEE80211_HW_AMPDU_AGGREGATION;
  848. hw->extra_tx_headroom = brcms_c_get_header_len();
  849. hw->queues = N_TX_QUEUES;
  850. hw->max_rates = 2; /* Primary rate and 1 fallback rate */
  851. /* channel change time is dependent on chip and band */
  852. hw->channel_change_time = 7 * 1000;
  853. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  854. hw->rate_control_algorithm = "minstrel_ht";
  855. hw->sta_data_size = 0;
  856. return ieee_hw_rate_init(hw);
  857. }
  858. /**
  859. * attach to the WL device.
  860. *
  861. * Attach to the WL device identified by vendor and device parameters.
  862. * regs is a host accessible memory address pointing to WL device registers.
  863. *
  864. * brcms_attach is not defined as static because in the case where no bus
  865. * is defined, wl_attach will never be called, and thus, gcc will issue
  866. * a warning that this function is defined but not used if we declare
  867. * it as static.
  868. *
  869. *
  870. * is called in brcms_bcma_probe() context, therefore no locking required.
  871. */
  872. static struct brcms_info *brcms_attach(struct bcma_device *pdev)
  873. {
  874. struct brcms_info *wl = NULL;
  875. int unit, err;
  876. struct ieee80211_hw *hw;
  877. u8 perm[ETH_ALEN];
  878. unit = n_adapters_found;
  879. err = 0;
  880. if (unit < 0)
  881. return NULL;
  882. /* allocate private info */
  883. hw = bcma_get_drvdata(pdev);
  884. if (hw != NULL)
  885. wl = hw->priv;
  886. if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
  887. return NULL;
  888. wl->wiphy = hw->wiphy;
  889. atomic_set(&wl->callbacks, 0);
  890. /* setup the bottom half handler */
  891. tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
  892. spin_lock_init(&wl->lock);
  893. spin_lock_init(&wl->isr_lock);
  894. /* prepare ucode */
  895. if (brcms_request_fw(wl, pdev->bus->host_pci) < 0) {
  896. wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
  897. "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
  898. brcms_release_fw(wl);
  899. brcms_remove(pdev);
  900. return NULL;
  901. }
  902. /* common load-time initialization */
  903. wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
  904. brcms_release_fw(wl);
  905. if (!wl->wlc) {
  906. wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
  907. KBUILD_MODNAME, err);
  908. goto fail;
  909. }
  910. wl->pub = brcms_c_pub(wl->wlc);
  911. wl->pub->ieee_hw = hw;
  912. /* register our interrupt handler */
  913. if (request_irq(pdev->bus->host_pci->irq, brcms_isr,
  914. IRQF_SHARED, KBUILD_MODNAME, wl)) {
  915. wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
  916. goto fail;
  917. }
  918. wl->irq = pdev->bus->host_pci->irq;
  919. /* register module */
  920. brcms_c_module_register(wl->pub, "linux", wl, NULL);
  921. if (ieee_hw_init(hw)) {
  922. wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
  923. __func__);
  924. goto fail;
  925. }
  926. memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
  927. if (WARN_ON(!is_valid_ether_addr(perm)))
  928. goto fail;
  929. SET_IEEE80211_PERM_ADDR(hw, perm);
  930. err = ieee80211_register_hw(hw);
  931. if (err)
  932. wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
  933. "%d\n", __func__, err);
  934. if (wl->pub->srom_ccode[0])
  935. err = brcms_set_hint(wl, wl->pub->srom_ccode);
  936. else
  937. err = brcms_set_hint(wl, "US");
  938. if (err)
  939. wiphy_err(wl->wiphy, "%s: regulatory_hint failed, status %d\n",
  940. __func__, err);
  941. n_adapters_found++;
  942. return wl;
  943. fail:
  944. brcms_free(wl);
  945. return NULL;
  946. }
  947. /**
  948. * determines if a device is a WL device, and if so, attaches it.
  949. *
  950. * This function determines if a device pointed to by pdev is a WL device,
  951. * and if so, performs a brcms_attach() on it.
  952. *
  953. * Perimeter lock is initialized in the course of this function.
  954. */
  955. static int __devinit brcms_bcma_probe(struct bcma_device *pdev)
  956. {
  957. struct brcms_info *wl;
  958. struct ieee80211_hw *hw;
  959. dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
  960. pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
  961. pdev->bus->host_pci->irq);
  962. if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
  963. (pdev->id.id != BCMA_CORE_80211))
  964. return -ENODEV;
  965. hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
  966. if (!hw) {
  967. pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
  968. return -ENOMEM;
  969. }
  970. SET_IEEE80211_DEV(hw, &pdev->dev);
  971. bcma_set_drvdata(pdev, hw);
  972. memset(hw->priv, 0, sizeof(*wl));
  973. wl = brcms_attach(pdev);
  974. if (!wl) {
  975. pr_err("%s: brcms_attach failed!\n", __func__);
  976. return -ENODEV;
  977. }
  978. return 0;
  979. }
  980. static int brcms_suspend(struct bcma_device *pdev)
  981. {
  982. struct brcms_info *wl;
  983. struct ieee80211_hw *hw;
  984. hw = bcma_get_drvdata(pdev);
  985. wl = hw->priv;
  986. if (!wl) {
  987. pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
  988. __func__);
  989. return -ENODEV;
  990. }
  991. /* only need to flag hw is down for proper resume */
  992. spin_lock_bh(&wl->lock);
  993. wl->pub->hw_up = false;
  994. spin_unlock_bh(&wl->lock);
  995. pr_debug("brcms_suspend ok\n");
  996. return 0;
  997. }
  998. static int brcms_resume(struct bcma_device *pdev)
  999. {
  1000. pr_debug("brcms_resume ok\n");
  1001. return 0;
  1002. }
  1003. static struct bcma_driver brcms_bcma_driver = {
  1004. .name = KBUILD_MODNAME,
  1005. .probe = brcms_bcma_probe,
  1006. .suspend = brcms_suspend,
  1007. .resume = brcms_resume,
  1008. .remove = __devexit_p(brcms_remove),
  1009. .id_table = brcms_coreid_table,
  1010. };
  1011. /**
  1012. * This is the main entry point for the brcmsmac driver.
  1013. *
  1014. * This function is scheduled upon module initialization and
  1015. * does the driver registration, which result in brcms_bcma_probe()
  1016. * call resulting in the driver bringup.
  1017. */
  1018. static void brcms_driver_init(struct work_struct *work)
  1019. {
  1020. int error;
  1021. error = bcma_driver_register(&brcms_bcma_driver);
  1022. if (error)
  1023. pr_err("%s: register returned %d\n", __func__, error);
  1024. }
  1025. static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
  1026. static int __init brcms_module_init(void)
  1027. {
  1028. #ifdef DEBUG
  1029. if (msglevel != 0xdeadbeef)
  1030. brcm_msg_level = msglevel;
  1031. #endif
  1032. if (!schedule_work(&brcms_driver_work))
  1033. return -EBUSY;
  1034. return 0;
  1035. }
  1036. /**
  1037. * This function unloads the brcmsmac driver from the system.
  1038. *
  1039. * This function unconditionally unloads the brcmsmac driver module from the
  1040. * system.
  1041. *
  1042. */
  1043. static void __exit brcms_module_exit(void)
  1044. {
  1045. cancel_work_sync(&brcms_driver_work);
  1046. bcma_driver_unregister(&brcms_bcma_driver);
  1047. }
  1048. module_init(brcms_module_init);
  1049. module_exit(brcms_module_exit);
  1050. /*
  1051. * precondition: perimeter lock has been acquired
  1052. */
  1053. void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
  1054. bool state, int prio)
  1055. {
  1056. wiphy_err(wl->wiphy, "Shouldn't be here %s\n", __func__);
  1057. }
  1058. /*
  1059. * precondition: perimeter lock has been acquired
  1060. */
  1061. void brcms_init(struct brcms_info *wl)
  1062. {
  1063. BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
  1064. brcms_reset(wl);
  1065. brcms_c_init(wl->wlc, wl->mute_tx);
  1066. }
  1067. /*
  1068. * precondition: perimeter lock has been acquired
  1069. */
  1070. uint brcms_reset(struct brcms_info *wl)
  1071. {
  1072. BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
  1073. brcms_c_reset(wl->wlc);
  1074. /* dpc will not be rescheduled */
  1075. wl->resched = false;
  1076. return 0;
  1077. }
  1078. void brcms_fatal_error(struct brcms_info *wl)
  1079. {
  1080. wiphy_err(wl->wlc->wiphy, "wl%d: fatal error, reinitializing\n",
  1081. wl->wlc->pub->unit);
  1082. brcms_reset(wl);
  1083. ieee80211_restart_hw(wl->pub->ieee_hw);
  1084. }
  1085. /*
  1086. * These are interrupt on/off entry points. Disable interrupts
  1087. * during interrupt state transition.
  1088. */
  1089. void brcms_intrson(struct brcms_info *wl)
  1090. {
  1091. unsigned long flags;
  1092. spin_lock_irqsave(&wl->isr_lock, flags);
  1093. brcms_c_intrson(wl->wlc);
  1094. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1095. }
  1096. u32 brcms_intrsoff(struct brcms_info *wl)
  1097. {
  1098. unsigned long flags;
  1099. u32 status;
  1100. spin_lock_irqsave(&wl->isr_lock, flags);
  1101. status = brcms_c_intrsoff(wl->wlc);
  1102. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1103. return status;
  1104. }
  1105. void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
  1106. {
  1107. unsigned long flags;
  1108. spin_lock_irqsave(&wl->isr_lock, flags);
  1109. brcms_c_intrsrestore(wl->wlc, macintmask);
  1110. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1111. }
  1112. /*
  1113. * precondition: perimeter lock has been acquired
  1114. */
  1115. int brcms_up(struct brcms_info *wl)
  1116. {
  1117. int error = 0;
  1118. if (wl->pub->up)
  1119. return 0;
  1120. error = brcms_c_up(wl->wlc);
  1121. return error;
  1122. }
  1123. /*
  1124. * precondition: perimeter lock has been acquired
  1125. */
  1126. void brcms_down(struct brcms_info *wl)
  1127. {
  1128. uint callbacks, ret_val = 0;
  1129. /* call common down function */
  1130. ret_val = brcms_c_down(wl->wlc);
  1131. callbacks = atomic_read(&wl->callbacks) - ret_val;
  1132. /* wait for down callbacks to complete */
  1133. spin_unlock_bh(&wl->lock);
  1134. /* For HIGH_only driver, it's important to actually schedule other work,
  1135. * not just spin wait since everything runs at schedule level
  1136. */
  1137. SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
  1138. spin_lock_bh(&wl->lock);
  1139. }
  1140. /*
  1141. * precondition: perimeter lock is not acquired
  1142. */
  1143. static void _brcms_timer(struct work_struct *work)
  1144. {
  1145. struct brcms_timer *t = container_of(work, struct brcms_timer,
  1146. dly_wrk.work);
  1147. spin_lock_bh(&t->wl->lock);
  1148. if (t->set) {
  1149. if (t->periodic) {
  1150. atomic_inc(&t->wl->callbacks);
  1151. ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
  1152. &t->dly_wrk,
  1153. msecs_to_jiffies(t->ms));
  1154. } else {
  1155. t->set = false;
  1156. }
  1157. t->fn(t->arg);
  1158. }
  1159. atomic_dec(&t->wl->callbacks);
  1160. spin_unlock_bh(&t->wl->lock);
  1161. }
  1162. /*
  1163. * Adds a timer to the list. Caller supplies a timer function.
  1164. * Is called from wlc.
  1165. *
  1166. * precondition: perimeter lock has been acquired
  1167. */
  1168. struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
  1169. void (*fn) (void *arg),
  1170. void *arg, const char *name)
  1171. {
  1172. struct brcms_timer *t;
  1173. t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
  1174. if (!t)
  1175. return NULL;
  1176. INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
  1177. t->wl = wl;
  1178. t->fn = fn;
  1179. t->arg = arg;
  1180. t->next = wl->timers;
  1181. wl->timers = t;
  1182. #ifdef DEBUG
  1183. t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
  1184. if (t->name)
  1185. strcpy(t->name, name);
  1186. #endif
  1187. return t;
  1188. }
  1189. /*
  1190. * adds only the kernel timer since it's going to be more accurate
  1191. * as well as it's easier to make it periodic
  1192. *
  1193. * precondition: perimeter lock has been acquired
  1194. */
  1195. void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
  1196. {
  1197. struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
  1198. #ifdef DEBUG
  1199. if (t->set)
  1200. wiphy_err(hw->wiphy, "%s: Already set. Name: %s, per %d\n",
  1201. __func__, t->name, periodic);
  1202. #endif
  1203. t->ms = ms;
  1204. t->periodic = (bool) periodic;
  1205. t->set = true;
  1206. atomic_inc(&t->wl->callbacks);
  1207. ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
  1208. }
  1209. /*
  1210. * return true if timer successfully deleted, false if still pending
  1211. *
  1212. * precondition: perimeter lock has been acquired
  1213. */
  1214. bool brcms_del_timer(struct brcms_timer *t)
  1215. {
  1216. if (t->set) {
  1217. t->set = false;
  1218. if (!cancel_delayed_work(&t->dly_wrk))
  1219. return false;
  1220. atomic_dec(&t->wl->callbacks);
  1221. }
  1222. return true;
  1223. }
  1224. /*
  1225. * precondition: perimeter lock has been acquired
  1226. */
  1227. void brcms_free_timer(struct brcms_timer *t)
  1228. {
  1229. struct brcms_info *wl = t->wl;
  1230. struct brcms_timer *tmp;
  1231. /* delete the timer in case it is active */
  1232. brcms_del_timer(t);
  1233. if (wl->timers == t) {
  1234. wl->timers = wl->timers->next;
  1235. #ifdef DEBUG
  1236. kfree(t->name);
  1237. #endif
  1238. kfree(t);
  1239. return;
  1240. }
  1241. tmp = wl->timers;
  1242. while (tmp) {
  1243. if (tmp->next == t) {
  1244. tmp->next = t->next;
  1245. #ifdef DEBUG
  1246. kfree(t->name);
  1247. #endif
  1248. kfree(t);
  1249. return;
  1250. }
  1251. tmp = tmp->next;
  1252. }
  1253. }
  1254. /*
  1255. * precondition: perimeter lock has been acquired
  1256. */
  1257. int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
  1258. {
  1259. int i, entry;
  1260. const u8 *pdata;
  1261. struct firmware_hdr *hdr;
  1262. for (i = 0; i < wl->fw.fw_cnt; i++) {
  1263. hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
  1264. for (entry = 0; entry < wl->fw.hdr_num_entries[i];
  1265. entry++, hdr++) {
  1266. u32 len = le32_to_cpu(hdr->len);
  1267. if (le32_to_cpu(hdr->idx) == idx) {
  1268. pdata = wl->fw.fw_bin[i]->data +
  1269. le32_to_cpu(hdr->offset);
  1270. *pbuf = kmemdup(pdata, len, GFP_ATOMIC);
  1271. if (*pbuf == NULL)
  1272. goto fail;
  1273. return 0;
  1274. }
  1275. }
  1276. }
  1277. wiphy_err(wl->wiphy, "ERROR: ucode buf tag:%d can not be found!\n",
  1278. idx);
  1279. *pbuf = NULL;
  1280. fail:
  1281. return -ENODATA;
  1282. }
  1283. /*
  1284. * Precondition: Since this function is called in brcms_bcma_probe() context,
  1285. * no locking is required.
  1286. */
  1287. int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
  1288. {
  1289. int i, entry;
  1290. const u8 *pdata;
  1291. struct firmware_hdr *hdr;
  1292. for (i = 0; i < wl->fw.fw_cnt; i++) {
  1293. hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
  1294. for (entry = 0; entry < wl->fw.hdr_num_entries[i];
  1295. entry++, hdr++) {
  1296. if (le32_to_cpu(hdr->idx) == idx) {
  1297. pdata = wl->fw.fw_bin[i]->data +
  1298. le32_to_cpu(hdr->offset);
  1299. if (le32_to_cpu(hdr->len) != 4) {
  1300. wiphy_err(wl->wiphy,
  1301. "ERROR: fw hdr len\n");
  1302. return -ENOMSG;
  1303. }
  1304. *n_bytes = le32_to_cpu(*((__le32 *) pdata));
  1305. return 0;
  1306. }
  1307. }
  1308. }
  1309. wiphy_err(wl->wiphy, "ERROR: ucode tag:%d can not be found!\n", idx);
  1310. return -ENOMSG;
  1311. }
  1312. /*
  1313. * precondition: can both be called locked and unlocked
  1314. */
  1315. void brcms_ucode_free_buf(void *p)
  1316. {
  1317. kfree(p);
  1318. }
  1319. /*
  1320. * checks validity of all firmware images loaded from user space
  1321. *
  1322. * Precondition: Since this function is called in brcms_bcma_probe() context,
  1323. * no locking is required.
  1324. */
  1325. int brcms_check_firmwares(struct brcms_info *wl)
  1326. {
  1327. int i;
  1328. int entry;
  1329. int rc = 0;
  1330. const struct firmware *fw;
  1331. const struct firmware *fw_hdr;
  1332. struct firmware_hdr *ucode_hdr;
  1333. for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
  1334. fw = wl->fw.fw_bin[i];
  1335. fw_hdr = wl->fw.fw_hdr[i];
  1336. if (fw == NULL && fw_hdr == NULL) {
  1337. break;
  1338. } else if (fw == NULL || fw_hdr == NULL) {
  1339. wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
  1340. __func__);
  1341. rc = -EBADF;
  1342. } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
  1343. wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
  1344. "size %zu/%zu\n", __func__, fw_hdr->size,
  1345. sizeof(struct firmware_hdr));
  1346. rc = -EBADF;
  1347. } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
  1348. wiphy_err(wl->wiphy, "%s: out of bounds fw file size "
  1349. "%zu\n", __func__, fw->size);
  1350. rc = -EBADF;
  1351. } else {
  1352. /* check if ucode section overruns firmware image */
  1353. ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
  1354. for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
  1355. !rc; entry++, ucode_hdr++) {
  1356. if (le32_to_cpu(ucode_hdr->offset) +
  1357. le32_to_cpu(ucode_hdr->len) >
  1358. fw->size) {
  1359. wiphy_err(wl->wiphy,
  1360. "%s: conflicting bin/hdr\n",
  1361. __func__);
  1362. rc = -EBADF;
  1363. }
  1364. }
  1365. }
  1366. }
  1367. if (rc == 0 && wl->fw.fw_cnt != i) {
  1368. wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
  1369. wl->fw.fw_cnt);
  1370. rc = -EBADF;
  1371. }
  1372. return rc;
  1373. }
  1374. /*
  1375. * precondition: perimeter lock has been acquired
  1376. */
  1377. bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
  1378. {
  1379. bool blocked = brcms_c_check_radio_disabled(wl->wlc);
  1380. spin_unlock_bh(&wl->lock);
  1381. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
  1382. if (blocked)
  1383. wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
  1384. spin_lock_bh(&wl->lock);
  1385. return blocked;
  1386. }
  1387. /*
  1388. * precondition: perimeter lock has been acquired
  1389. */
  1390. void brcms_msleep(struct brcms_info *wl, uint ms)
  1391. {
  1392. spin_unlock_bh(&wl->lock);
  1393. msleep(ms);
  1394. spin_lock_bh(&wl->lock);
  1395. }