mac80211_if.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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. #include <linux/etherdevice.h>
  18. #include <linux/pci.h>
  19. #include <linux/sched.h>
  20. #include <linux/firmware.h>
  21. #include <linux/interrupt.h>
  22. #include <net/mac80211.h>
  23. #include <defs.h>
  24. #include "nicpci.h"
  25. #include "phy/phy_int.h"
  26. #include "d11.h"
  27. #include "channel.h"
  28. #include "scb.h"
  29. #include "pub.h"
  30. #include "ucode_loader.h"
  31. #include "mac80211_if.h"
  32. #include "main.h"
  33. #define N_TX_QUEUES 4 /* #tx queues on mac80211<->driver interface */
  34. /* Flags we support */
  35. #define MAC_FILTERS (FIF_PROMISC_IN_BSS | \
  36. FIF_ALLMULTI | \
  37. FIF_FCSFAIL | \
  38. FIF_PLCPFAIL | \
  39. FIF_CONTROL | \
  40. FIF_OTHER_BSS | \
  41. FIF_BCN_PRBRESP_PROMISC)
  42. #define CHAN2GHZ(channel, freqency, chflags) { \
  43. .band = IEEE80211_BAND_2GHZ, \
  44. .center_freq = (freqency), \
  45. .hw_value = (channel), \
  46. .flags = chflags, \
  47. .max_antenna_gain = 0, \
  48. .max_power = 19, \
  49. }
  50. #define CHAN5GHZ(channel, chflags) { \
  51. .band = IEEE80211_BAND_5GHZ, \
  52. .center_freq = 5000 + 5*(channel), \
  53. .hw_value = (channel), \
  54. .flags = chflags, \
  55. .max_antenna_gain = 0, \
  56. .max_power = 21, \
  57. }
  58. #define RATE(rate100m, _flags) { \
  59. .bitrate = (rate100m), \
  60. .flags = (_flags), \
  61. .hw_value = (rate100m / 5), \
  62. }
  63. struct firmware_hdr {
  64. __le32 offset;
  65. __le32 len;
  66. __le32 idx;
  67. };
  68. static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
  69. "brcm/bcm43xx",
  70. NULL
  71. };
  72. static int n_adapters_found;
  73. MODULE_AUTHOR("Broadcom Corporation");
  74. MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
  75. MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
  76. MODULE_LICENSE("Dual BSD/GPL");
  77. /* recognized PCI IDs */
  78. static DEFINE_PCI_DEVICE_TABLE(brcms_pci_id_table) = {
  79. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) }, /* 43225 2G */
  80. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) }, /* 43224 DUAL */
  81. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) }, /* 4313 DUAL */
  82. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) }, /* 43224 Ven */
  83. {0}
  84. };
  85. MODULE_DEVICE_TABLE(pci, brcms_pci_id_table);
  86. #ifdef BCMDBG
  87. static int msglevel = 0xdeadbeef;
  88. module_param(msglevel, int, 0);
  89. #endif /* BCMDBG */
  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_AP &&
  309. vif->type != NL80211_IFTYPE_MESH_POINT &&
  310. vif->type != NL80211_IFTYPE_STATION &&
  311. vif->type != NL80211_IFTYPE_WDS &&
  312. vif->type != NL80211_IFTYPE_ADHOC) {
  313. wiphy_err(hw->wiphy, "%s: Attempt to add type %d, only"
  314. " STA for now\n", __func__, vif->type);
  315. return -EOPNOTSUPP;
  316. }
  317. wl->mute_tx = false;
  318. brcms_c_mute(wl->wlc, false);
  319. return 0;
  320. }
  321. static void
  322. brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  323. {
  324. }
  325. static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
  326. {
  327. struct ieee80211_conf *conf = &hw->conf;
  328. struct brcms_info *wl = hw->priv;
  329. int err = 0;
  330. int new_int;
  331. struct wiphy *wiphy = hw->wiphy;
  332. spin_lock_bh(&wl->lock);
  333. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  334. brcms_c_set_beacon_listen_interval(wl->wlc,
  335. conf->listen_interval);
  336. }
  337. if (changed & IEEE80211_CONF_CHANGE_MONITOR)
  338. wiphy_err(wiphy, "%s: change monitor mode: %s (implement)\n",
  339. __func__, conf->flags & IEEE80211_CONF_MONITOR ?
  340. "true" : "false");
  341. if (changed & IEEE80211_CONF_CHANGE_PS)
  342. wiphy_err(wiphy, "%s: change power-save mode: %s (implement)\n",
  343. __func__, conf->flags & IEEE80211_CONF_PS ?
  344. "true" : "false");
  345. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  346. err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
  347. if (err < 0) {
  348. wiphy_err(wiphy, "%s: Error setting power_level\n",
  349. __func__);
  350. goto config_out;
  351. }
  352. new_int = brcms_c_get_tx_power(wl->wlc);
  353. if (new_int != conf->power_level)
  354. wiphy_err(wiphy, "%s: Power level req != actual, %d %d"
  355. "\n", __func__, conf->power_level,
  356. new_int);
  357. }
  358. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  359. if (conf->channel_type == NL80211_CHAN_HT20 ||
  360. conf->channel_type == NL80211_CHAN_NO_HT)
  361. err = brcms_c_set_channel(wl->wlc,
  362. conf->channel->hw_value);
  363. else
  364. err = -ENOTSUPP;
  365. }
  366. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  367. err = brcms_c_set_rate_limit(wl->wlc,
  368. conf->short_frame_max_tx_count,
  369. conf->long_frame_max_tx_count);
  370. config_out:
  371. spin_unlock_bh(&wl->lock);
  372. return err;
  373. }
  374. static void
  375. brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
  376. struct ieee80211_vif *vif,
  377. struct ieee80211_bss_conf *info, u32 changed)
  378. {
  379. struct brcms_info *wl = hw->priv;
  380. struct wiphy *wiphy = hw->wiphy;
  381. if (changed & BSS_CHANGED_ASSOC) {
  382. /* association status changed (associated/disassociated)
  383. * also implies a change in the AID.
  384. */
  385. wiphy_err(wiphy, "%s: %s: %sassociated\n", KBUILD_MODNAME,
  386. __func__, info->assoc ? "" : "dis");
  387. spin_lock_bh(&wl->lock);
  388. brcms_c_associate_upd(wl->wlc, info->assoc);
  389. spin_unlock_bh(&wl->lock);
  390. }
  391. if (changed & BSS_CHANGED_ERP_SLOT) {
  392. s8 val;
  393. /* slot timing changed */
  394. if (info->use_short_slot)
  395. val = 1;
  396. else
  397. val = 0;
  398. spin_lock_bh(&wl->lock);
  399. brcms_c_set_shortslot_override(wl->wlc, val);
  400. spin_unlock_bh(&wl->lock);
  401. }
  402. if (changed & BSS_CHANGED_HT) {
  403. /* 802.11n parameters changed */
  404. u16 mode = info->ht_operation_mode;
  405. spin_lock_bh(&wl->lock);
  406. brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
  407. mode & IEEE80211_HT_OP_MODE_PROTECTION);
  408. brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
  409. mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  410. brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
  411. mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
  412. spin_unlock_bh(&wl->lock);
  413. }
  414. if (changed & BSS_CHANGED_BASIC_RATES) {
  415. struct ieee80211_supported_band *bi;
  416. u32 br_mask, i;
  417. u16 rate;
  418. struct brcm_rateset rs;
  419. int error;
  420. /* retrieve the current rates */
  421. spin_lock_bh(&wl->lock);
  422. brcms_c_get_current_rateset(wl->wlc, &rs);
  423. spin_unlock_bh(&wl->lock);
  424. br_mask = info->basic_rates;
  425. bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
  426. for (i = 0; i < bi->n_bitrates; i++) {
  427. /* convert to internal rate value */
  428. rate = (bi->bitrates[i].bitrate << 1) / 10;
  429. /* set/clear basic rate flag */
  430. brcms_set_basic_rate(&rs, rate, br_mask & 1);
  431. br_mask >>= 1;
  432. }
  433. /* update the rate set */
  434. spin_lock_bh(&wl->lock);
  435. error = brcms_c_set_rateset(wl->wlc, &rs);
  436. spin_unlock_bh(&wl->lock);
  437. if (error)
  438. wiphy_err(wiphy, "changing basic rates failed: %d\n",
  439. error);
  440. }
  441. if (changed & BSS_CHANGED_BEACON_INT) {
  442. /* Beacon interval changed */
  443. spin_lock_bh(&wl->lock);
  444. brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
  445. spin_unlock_bh(&wl->lock);
  446. }
  447. if (changed & BSS_CHANGED_BSSID) {
  448. /* BSSID changed, for whatever reason (IBSS and managed mode) */
  449. spin_lock_bh(&wl->lock);
  450. brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
  451. spin_unlock_bh(&wl->lock);
  452. }
  453. if (changed & BSS_CHANGED_BEACON)
  454. /* Beacon data changed, retrieve new beacon (beaconing modes) */
  455. wiphy_err(wiphy, "%s: beacon changed\n", __func__);
  456. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  457. /* Beaconing should be enabled/disabled (beaconing modes) */
  458. wiphy_err(wiphy, "%s: Beacon enabled: %s\n", __func__,
  459. info->enable_beacon ? "true" : "false");
  460. }
  461. if (changed & BSS_CHANGED_CQM) {
  462. /* Connection quality monitor config changed */
  463. wiphy_err(wiphy, "%s: cqm change: threshold %d, hys %d "
  464. " (implement)\n", __func__, info->cqm_rssi_thold,
  465. info->cqm_rssi_hyst);
  466. }
  467. if (changed & BSS_CHANGED_IBSS) {
  468. /* IBSS join status changed */
  469. wiphy_err(wiphy, "%s: IBSS joined: %s (implement)\n", __func__,
  470. info->ibss_joined ? "true" : "false");
  471. }
  472. if (changed & BSS_CHANGED_ARP_FILTER) {
  473. /* Hardware ARP filter address list or state changed */
  474. wiphy_err(wiphy, "%s: arp filtering: enabled %s, count %d"
  475. " (implement)\n", __func__, info->arp_filter_enabled ?
  476. "true" : "false", info->arp_addr_cnt);
  477. }
  478. if (changed & BSS_CHANGED_QOS) {
  479. /*
  480. * QoS for this association was enabled/disabled.
  481. * Note that it is only ever disabled for station mode.
  482. */
  483. wiphy_err(wiphy, "%s: qos enabled: %s (implement)\n", __func__,
  484. info->qos ? "true" : "false");
  485. }
  486. return;
  487. }
  488. static void
  489. brcms_ops_configure_filter(struct ieee80211_hw *hw,
  490. unsigned int changed_flags,
  491. unsigned int *total_flags, u64 multicast)
  492. {
  493. struct brcms_info *wl = hw->priv;
  494. struct wiphy *wiphy = hw->wiphy;
  495. changed_flags &= MAC_FILTERS;
  496. *total_flags &= MAC_FILTERS;
  497. if (changed_flags & FIF_PROMISC_IN_BSS)
  498. wiphy_err(wiphy, "FIF_PROMISC_IN_BSS\n");
  499. if (changed_flags & FIF_ALLMULTI)
  500. wiphy_err(wiphy, "FIF_ALLMULTI\n");
  501. if (changed_flags & FIF_FCSFAIL)
  502. wiphy_err(wiphy, "FIF_FCSFAIL\n");
  503. if (changed_flags & FIF_PLCPFAIL)
  504. wiphy_err(wiphy, "FIF_PLCPFAIL\n");
  505. if (changed_flags & FIF_CONTROL)
  506. wiphy_err(wiphy, "FIF_CONTROL\n");
  507. if (changed_flags & FIF_OTHER_BSS)
  508. wiphy_err(wiphy, "FIF_OTHER_BSS\n");
  509. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  510. spin_lock_bh(&wl->lock);
  511. if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
  512. wl->pub->mac80211_state |= MAC80211_PROMISC_BCNS;
  513. brcms_c_mac_bcn_promisc_change(wl->wlc, 1);
  514. } else {
  515. brcms_c_mac_bcn_promisc_change(wl->wlc, 0);
  516. wl->pub->mac80211_state &= ~MAC80211_PROMISC_BCNS;
  517. }
  518. spin_unlock_bh(&wl->lock);
  519. }
  520. return;
  521. }
  522. static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw)
  523. {
  524. struct brcms_info *wl = hw->priv;
  525. spin_lock_bh(&wl->lock);
  526. brcms_c_scan_start(wl->wlc);
  527. spin_unlock_bh(&wl->lock);
  528. return;
  529. }
  530. static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw)
  531. {
  532. struct brcms_info *wl = hw->priv;
  533. spin_lock_bh(&wl->lock);
  534. brcms_c_scan_stop(wl->wlc);
  535. spin_unlock_bh(&wl->lock);
  536. return;
  537. }
  538. static int
  539. brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
  540. const struct ieee80211_tx_queue_params *params)
  541. {
  542. struct brcms_info *wl = hw->priv;
  543. spin_lock_bh(&wl->lock);
  544. brcms_c_wme_setparams(wl->wlc, queue, params, true);
  545. spin_unlock_bh(&wl->lock);
  546. return 0;
  547. }
  548. static int
  549. brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  550. struct ieee80211_sta *sta)
  551. {
  552. struct brcms_info *wl = hw->priv;
  553. struct scb *scb = &wl->wlc->pri_scb;
  554. brcms_c_init_scb(scb);
  555. wl->pub->global_ampdu = &(scb->scb_ampdu);
  556. wl->pub->global_ampdu->scb = scb;
  557. wl->pub->global_ampdu->max_pdu = 16;
  558. sta->ht_cap.ht_supported = true;
  559. sta->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  560. sta->ht_cap.ampdu_density = AMPDU_DEF_MPDU_DENSITY;
  561. sta->ht_cap.cap = IEEE80211_HT_CAP_GRN_FLD |
  562. IEEE80211_HT_CAP_SGI_20 |
  563. IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_40MHZ_INTOLERANT;
  564. /*
  565. * minstrel_ht initiates addBA on our behalf by calling
  566. * ieee80211_start_tx_ba_session()
  567. */
  568. return 0;
  569. }
  570. static int
  571. brcms_ops_ampdu_action(struct ieee80211_hw *hw,
  572. struct ieee80211_vif *vif,
  573. enum ieee80211_ampdu_mlme_action action,
  574. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  575. u8 buf_size)
  576. {
  577. struct brcms_info *wl = hw->priv;
  578. struct scb *scb = &wl->wlc->pri_scb;
  579. int status;
  580. if (WARN_ON(scb->magic != SCB_MAGIC))
  581. return -EIDRM;
  582. switch (action) {
  583. case IEEE80211_AMPDU_RX_START:
  584. break;
  585. case IEEE80211_AMPDU_RX_STOP:
  586. break;
  587. case IEEE80211_AMPDU_TX_START:
  588. spin_lock_bh(&wl->lock);
  589. status = brcms_c_aggregatable(wl->wlc, tid);
  590. spin_unlock_bh(&wl->lock);
  591. if (!status) {
  592. wiphy_err(wl->wiphy, "START: tid %d is not agg\'able\n",
  593. tid);
  594. return -EINVAL;
  595. }
  596. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  597. break;
  598. case IEEE80211_AMPDU_TX_STOP:
  599. spin_lock_bh(&wl->lock);
  600. brcms_c_ampdu_flush(wl->wlc, sta, tid);
  601. spin_unlock_bh(&wl->lock);
  602. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  603. break;
  604. case IEEE80211_AMPDU_TX_OPERATIONAL:
  605. /*
  606. * BA window size from ADDBA response ('buf_size') defines how
  607. * many outstanding MPDUs are allowed for the BA stream by
  608. * recipient and traffic class. 'ampdu_factor' gives maximum
  609. * AMPDU size.
  610. */
  611. spin_lock_bh(&wl->lock);
  612. brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
  613. (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  614. sta->ht_cap.ampdu_factor)) - 1);
  615. spin_unlock_bh(&wl->lock);
  616. /* Power save wakeup */
  617. break;
  618. default:
  619. wiphy_err(wl->wiphy, "%s: Invalid command, ignoring\n",
  620. __func__);
  621. }
  622. return 0;
  623. }
  624. static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
  625. {
  626. struct brcms_info *wl = hw->priv;
  627. bool blocked;
  628. spin_lock_bh(&wl->lock);
  629. blocked = brcms_c_check_radio_disabled(wl->wlc);
  630. spin_unlock_bh(&wl->lock);
  631. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
  632. }
  633. static void brcms_ops_flush(struct ieee80211_hw *hw, bool drop)
  634. {
  635. struct brcms_info *wl = hw->priv;
  636. no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
  637. /* wait for packet queue and dma fifos to run empty */
  638. spin_lock_bh(&wl->lock);
  639. brcms_c_wait_for_tx_completion(wl->wlc, drop);
  640. spin_unlock_bh(&wl->lock);
  641. }
  642. static const struct ieee80211_ops brcms_ops = {
  643. .tx = brcms_ops_tx,
  644. .start = brcms_ops_start,
  645. .stop = brcms_ops_stop,
  646. .add_interface = brcms_ops_add_interface,
  647. .remove_interface = brcms_ops_remove_interface,
  648. .config = brcms_ops_config,
  649. .bss_info_changed = brcms_ops_bss_info_changed,
  650. .configure_filter = brcms_ops_configure_filter,
  651. .sw_scan_start = brcms_ops_sw_scan_start,
  652. .sw_scan_complete = brcms_ops_sw_scan_complete,
  653. .conf_tx = brcms_ops_conf_tx,
  654. .sta_add = brcms_ops_sta_add,
  655. .ampdu_action = brcms_ops_ampdu_action,
  656. .rfkill_poll = brcms_ops_rfkill_poll,
  657. .flush = brcms_ops_flush,
  658. };
  659. /*
  660. * is called in brcms_pci_probe() context, therefore no locking required.
  661. */
  662. static int brcms_set_hint(struct brcms_info *wl, char *abbrev)
  663. {
  664. return regulatory_hint(wl->pub->ieee_hw->wiphy, abbrev);
  665. }
  666. void brcms_dpc(unsigned long data)
  667. {
  668. struct brcms_info *wl;
  669. wl = (struct brcms_info *) data;
  670. spin_lock_bh(&wl->lock);
  671. /* call the common second level interrupt handler */
  672. if (wl->pub->up) {
  673. if (wl->resched) {
  674. unsigned long flags;
  675. spin_lock_irqsave(&wl->isr_lock, flags);
  676. brcms_c_intrsupd(wl->wlc);
  677. spin_unlock_irqrestore(&wl->isr_lock, flags);
  678. }
  679. wl->resched = brcms_c_dpc(wl->wlc, true);
  680. }
  681. /* brcms_c_dpc() may bring the driver down */
  682. if (!wl->pub->up)
  683. goto done;
  684. /* re-schedule dpc */
  685. if (wl->resched)
  686. tasklet_schedule(&wl->tasklet);
  687. else
  688. /* re-enable interrupts */
  689. brcms_intrson(wl);
  690. done:
  691. spin_unlock_bh(&wl->lock);
  692. }
  693. /*
  694. * Precondition: Since this function is called in brcms_pci_probe() context,
  695. * no locking is required.
  696. */
  697. static int brcms_request_fw(struct brcms_info *wl, struct pci_dev *pdev)
  698. {
  699. int status;
  700. struct device *device = &pdev->dev;
  701. char fw_name[100];
  702. int i;
  703. memset(&wl->fw, 0, sizeof(struct brcms_firmware));
  704. for (i = 0; i < MAX_FW_IMAGES; i++) {
  705. if (brcms_firmwares[i] == NULL)
  706. break;
  707. sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
  708. UCODE_LOADER_API_VER);
  709. status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
  710. if (status) {
  711. wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
  712. KBUILD_MODNAME, fw_name);
  713. return status;
  714. }
  715. sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
  716. UCODE_LOADER_API_VER);
  717. status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
  718. if (status) {
  719. wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
  720. KBUILD_MODNAME, fw_name);
  721. return status;
  722. }
  723. wl->fw.hdr_num_entries[i] =
  724. wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
  725. }
  726. wl->fw.fw_cnt = i;
  727. return brcms_ucode_data_init(wl, &wl->ucode);
  728. }
  729. /*
  730. * Precondition: Since this function is called in brcms_pci_probe() context,
  731. * no locking is required.
  732. */
  733. static void brcms_release_fw(struct brcms_info *wl)
  734. {
  735. int i;
  736. for (i = 0; i < MAX_FW_IMAGES; i++) {
  737. release_firmware(wl->fw.fw_bin[i]);
  738. release_firmware(wl->fw.fw_hdr[i]);
  739. }
  740. }
  741. /**
  742. * This function frees the WL per-device resources.
  743. *
  744. * This function frees resources owned by the WL device pointed to
  745. * by the wl parameter.
  746. *
  747. * precondition: can both be called locked and unlocked
  748. *
  749. */
  750. static void brcms_free(struct brcms_info *wl)
  751. {
  752. struct brcms_timer *t, *next;
  753. /* free ucode data */
  754. if (wl->fw.fw_cnt)
  755. brcms_ucode_data_free(&wl->ucode);
  756. if (wl->irq)
  757. free_irq(wl->irq, wl);
  758. /* kill dpc */
  759. tasklet_kill(&wl->tasklet);
  760. if (wl->pub)
  761. brcms_c_module_unregister(wl->pub, "linux", wl);
  762. /* free common resources */
  763. if (wl->wlc) {
  764. brcms_c_detach(wl->wlc);
  765. wl->wlc = NULL;
  766. wl->pub = NULL;
  767. }
  768. /* virtual interface deletion is deferred so we cannot spinwait */
  769. /* wait for all pending callbacks to complete */
  770. while (atomic_read(&wl->callbacks) > 0)
  771. schedule();
  772. /* free timers */
  773. for (t = wl->timers; t; t = next) {
  774. next = t->next;
  775. #ifdef BCMDBG
  776. kfree(t->name);
  777. #endif
  778. kfree(t);
  779. }
  780. /*
  781. * unregister_netdev() calls get_stats() which may read chip
  782. * registers so we cannot unmap the chip registers until
  783. * after calling unregister_netdev() .
  784. */
  785. if (wl->regsva)
  786. iounmap(wl->regsva);
  787. wl->regsva = NULL;
  788. }
  789. /*
  790. * called from both kernel as from this kernel module (error flow on attach)
  791. * precondition: perimeter lock is not acquired.
  792. */
  793. static void brcms_remove(struct pci_dev *pdev)
  794. {
  795. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  796. struct brcms_info *wl = hw->priv;
  797. if (wl->wlc) {
  798. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
  799. wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
  800. ieee80211_unregister_hw(hw);
  801. }
  802. pci_disable_device(pdev);
  803. brcms_free(wl);
  804. pci_set_drvdata(pdev, NULL);
  805. ieee80211_free_hw(hw);
  806. }
  807. static irqreturn_t brcms_isr(int irq, void *dev_id)
  808. {
  809. struct brcms_info *wl;
  810. bool ours, wantdpc;
  811. wl = (struct brcms_info *) dev_id;
  812. spin_lock(&wl->isr_lock);
  813. /* call common first level interrupt handler */
  814. ours = brcms_c_isr(wl->wlc, &wantdpc);
  815. if (ours) {
  816. /* if more to do... */
  817. if (wantdpc) {
  818. /* ...and call the second level interrupt handler */
  819. /* schedule dpc */
  820. tasklet_schedule(&wl->tasklet);
  821. }
  822. }
  823. spin_unlock(&wl->isr_lock);
  824. return IRQ_RETVAL(ours);
  825. }
  826. /*
  827. * is called in brcms_pci_probe() context, therefore no locking required.
  828. */
  829. static int ieee_hw_rate_init(struct ieee80211_hw *hw)
  830. {
  831. struct brcms_info *wl = hw->priv;
  832. struct brcms_c_info *wlc = wl->wlc;
  833. struct ieee80211_supported_band *band;
  834. int has_5g = 0;
  835. u16 phy_type;
  836. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  837. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  838. phy_type = brcms_c_get_phy_type(wl->wlc, 0);
  839. if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
  840. band = &wlc->bandstate[BAND_2G_INDEX]->band;
  841. *band = brcms_band_2GHz_nphy_template;
  842. if (phy_type == PHY_TYPE_LCN) {
  843. /* Single stream */
  844. band->ht_cap.mcs.rx_mask[1] = 0;
  845. band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
  846. }
  847. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  848. } else {
  849. return -EPERM;
  850. }
  851. /* Assume all bands use the same phy. True for 11n devices. */
  852. if (wl->pub->_nbands > 1) {
  853. has_5g++;
  854. if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
  855. band = &wlc->bandstate[BAND_5G_INDEX]->band;
  856. *band = brcms_band_5GHz_nphy_template;
  857. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  858. } else {
  859. return -EPERM;
  860. }
  861. }
  862. return 0;
  863. }
  864. /*
  865. * is called in brcms_pci_probe() context, therefore no locking required.
  866. */
  867. static int ieee_hw_init(struct ieee80211_hw *hw)
  868. {
  869. hw->flags = IEEE80211_HW_SIGNAL_DBM
  870. /* | IEEE80211_HW_CONNECTION_MONITOR What is this? */
  871. | IEEE80211_HW_REPORTS_TX_ACK_STATUS
  872. | IEEE80211_HW_AMPDU_AGGREGATION;
  873. hw->extra_tx_headroom = brcms_c_get_header_len();
  874. hw->queues = N_TX_QUEUES;
  875. hw->max_rates = 2; /* Primary rate and 1 fallback rate */
  876. /* channel change time is dependent on chip and band */
  877. hw->channel_change_time = 7 * 1000;
  878. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  879. hw->rate_control_algorithm = "minstrel_ht";
  880. hw->sta_data_size = 0;
  881. return ieee_hw_rate_init(hw);
  882. }
  883. /**
  884. * attach to the WL device.
  885. *
  886. * Attach to the WL device identified by vendor and device parameters.
  887. * regs is a host accessible memory address pointing to WL device registers.
  888. *
  889. * brcms_attach is not defined as static because in the case where no bus
  890. * is defined, wl_attach will never be called, and thus, gcc will issue
  891. * a warning that this function is defined but not used if we declare
  892. * it as static.
  893. *
  894. *
  895. * is called in brcms_pci_probe() context, therefore no locking required.
  896. */
  897. static struct brcms_info *brcms_attach(u16 vendor, u16 device,
  898. resource_size_t regs,
  899. struct pci_dev *btparam, uint irq)
  900. {
  901. struct brcms_info *wl = NULL;
  902. int unit, err;
  903. struct ieee80211_hw *hw;
  904. u8 perm[ETH_ALEN];
  905. unit = n_adapters_found;
  906. err = 0;
  907. if (unit < 0)
  908. return NULL;
  909. /* allocate private info */
  910. hw = pci_get_drvdata(btparam); /* btparam == pdev */
  911. if (hw != NULL)
  912. wl = hw->priv;
  913. if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
  914. return NULL;
  915. wl->wiphy = hw->wiphy;
  916. atomic_set(&wl->callbacks, 0);
  917. /* setup the bottom half handler */
  918. tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
  919. wl->regsva = ioremap_nocache(regs, PCI_BAR0_WINSZ);
  920. if (wl->regsva == NULL) {
  921. wiphy_err(wl->wiphy, "wl%d: ioremap() failed\n", unit);
  922. goto fail;
  923. }
  924. spin_lock_init(&wl->lock);
  925. spin_lock_init(&wl->isr_lock);
  926. /* prepare ucode */
  927. if (brcms_request_fw(wl, btparam) < 0) {
  928. wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
  929. "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
  930. brcms_release_fw(wl);
  931. brcms_remove(btparam);
  932. return NULL;
  933. }
  934. /* common load-time initialization */
  935. wl->wlc = brcms_c_attach(wl, vendor, device, unit, false,
  936. wl->regsva, btparam, &err);
  937. brcms_release_fw(wl);
  938. if (!wl->wlc) {
  939. wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
  940. KBUILD_MODNAME, err);
  941. goto fail;
  942. }
  943. wl->pub = brcms_c_pub(wl->wlc);
  944. wl->pub->ieee_hw = hw;
  945. /* register our interrupt handler */
  946. if (request_irq(irq, brcms_isr, IRQF_SHARED, KBUILD_MODNAME, wl)) {
  947. wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
  948. goto fail;
  949. }
  950. wl->irq = irq;
  951. /* register module */
  952. brcms_c_module_register(wl->pub, "linux", wl, NULL);
  953. if (ieee_hw_init(hw)) {
  954. wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
  955. __func__);
  956. goto fail;
  957. }
  958. memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
  959. if (WARN_ON(!is_valid_ether_addr(perm)))
  960. goto fail;
  961. SET_IEEE80211_PERM_ADDR(hw, perm);
  962. err = ieee80211_register_hw(hw);
  963. if (err)
  964. wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
  965. "%d\n", __func__, err);
  966. if (wl->pub->srom_ccode[0])
  967. err = brcms_set_hint(wl, wl->pub->srom_ccode);
  968. else
  969. err = brcms_set_hint(wl, "US");
  970. if (err)
  971. wiphy_err(wl->wiphy, "%s: regulatory_hint failed, status %d\n",
  972. __func__, err);
  973. n_adapters_found++;
  974. return wl;
  975. fail:
  976. brcms_free(wl);
  977. return NULL;
  978. }
  979. /**
  980. * determines if a device is a WL device, and if so, attaches it.
  981. *
  982. * This function determines if a device pointed to by pdev is a WL device,
  983. * and if so, performs a brcms_attach() on it.
  984. *
  985. * Perimeter lock is initialized in the course of this function.
  986. */
  987. static int __devinit
  988. brcms_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  989. {
  990. int rc;
  991. struct brcms_info *wl;
  992. struct ieee80211_hw *hw;
  993. u32 val;
  994. dev_info(&pdev->dev, "bus %d slot %d func %d irq %d\n",
  995. pdev->bus->number, PCI_SLOT(pdev->devfn),
  996. PCI_FUNC(pdev->devfn), pdev->irq);
  997. if ((pdev->vendor != PCI_VENDOR_ID_BROADCOM) ||
  998. ((pdev->device != 0x0576) &&
  999. ((pdev->device & 0xff00) != 0x4300) &&
  1000. ((pdev->device & 0xff00) != 0x4700) &&
  1001. ((pdev->device < 43000) || (pdev->device > 43999))))
  1002. return -ENODEV;
  1003. rc = pci_enable_device(pdev);
  1004. if (rc) {
  1005. pr_err("%s: Cannot enable device %d-%d_%d\n",
  1006. __func__, pdev->bus->number, PCI_SLOT(pdev->devfn),
  1007. PCI_FUNC(pdev->devfn));
  1008. return -ENODEV;
  1009. }
  1010. pci_set_master(pdev);
  1011. pci_read_config_dword(pdev, 0x40, &val);
  1012. if ((val & 0x0000ff00) != 0)
  1013. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1014. hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
  1015. if (!hw) {
  1016. pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
  1017. return -ENOMEM;
  1018. }
  1019. SET_IEEE80211_DEV(hw, &pdev->dev);
  1020. pci_set_drvdata(pdev, hw);
  1021. memset(hw->priv, 0, sizeof(*wl));
  1022. wl = brcms_attach(pdev->vendor, pdev->device,
  1023. pci_resource_start(pdev, 0), pdev,
  1024. pdev->irq);
  1025. if (!wl) {
  1026. pr_err("%s: %s: brcms_attach failed!\n", KBUILD_MODNAME,
  1027. __func__);
  1028. return -ENODEV;
  1029. }
  1030. return 0;
  1031. }
  1032. static int brcms_suspend(struct pci_dev *pdev, pm_message_t state)
  1033. {
  1034. struct brcms_info *wl;
  1035. struct ieee80211_hw *hw;
  1036. hw = pci_get_drvdata(pdev);
  1037. wl = hw->priv;
  1038. if (!wl) {
  1039. wiphy_err(wl->wiphy,
  1040. "brcms_suspend: pci_get_drvdata failed\n");
  1041. return -ENODEV;
  1042. }
  1043. /* only need to flag hw is down for proper resume */
  1044. spin_lock_bh(&wl->lock);
  1045. wl->pub->hw_up = false;
  1046. spin_unlock_bh(&wl->lock);
  1047. pci_save_state(pdev);
  1048. pci_disable_device(pdev);
  1049. return pci_set_power_state(pdev, PCI_D3hot);
  1050. }
  1051. static int brcms_resume(struct pci_dev *pdev)
  1052. {
  1053. struct brcms_info *wl;
  1054. struct ieee80211_hw *hw;
  1055. int err = 0;
  1056. u32 val;
  1057. hw = pci_get_drvdata(pdev);
  1058. wl = hw->priv;
  1059. if (!wl) {
  1060. wiphy_err(wl->wiphy,
  1061. "wl: brcms_resume: pci_get_drvdata failed\n");
  1062. return -ENODEV;
  1063. }
  1064. err = pci_set_power_state(pdev, PCI_D0);
  1065. if (err)
  1066. return err;
  1067. pci_restore_state(pdev);
  1068. err = pci_enable_device(pdev);
  1069. if (err)
  1070. return err;
  1071. pci_set_master(pdev);
  1072. pci_read_config_dword(pdev, 0x40, &val);
  1073. if ((val & 0x0000ff00) != 0)
  1074. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1075. /*
  1076. * done. driver will be put in up state
  1077. * in brcms_ops_add_interface() call.
  1078. */
  1079. return err;
  1080. }
  1081. static struct pci_driver brcms_pci_driver = {
  1082. .name = KBUILD_MODNAME,
  1083. .probe = brcms_pci_probe,
  1084. .suspend = brcms_suspend,
  1085. .resume = brcms_resume,
  1086. .remove = __devexit_p(brcms_remove),
  1087. .id_table = brcms_pci_id_table,
  1088. };
  1089. /**
  1090. * This is the main entry point for the WL driver.
  1091. *
  1092. * This function determines if a device pointed to by pdev is a WL device,
  1093. * and if so, performs a brcms_attach() on it.
  1094. *
  1095. */
  1096. static int __init brcms_module_init(void)
  1097. {
  1098. int error = -ENODEV;
  1099. #ifdef BCMDBG
  1100. if (msglevel != 0xdeadbeef)
  1101. brcm_msg_level = msglevel;
  1102. #endif /* BCMDBG */
  1103. error = pci_register_driver(&brcms_pci_driver);
  1104. if (!error)
  1105. return 0;
  1106. return error;
  1107. }
  1108. /**
  1109. * This function unloads the WL driver from the system.
  1110. *
  1111. * This function unconditionally unloads the WL driver module from the
  1112. * system.
  1113. *
  1114. */
  1115. static void __exit brcms_module_exit(void)
  1116. {
  1117. pci_unregister_driver(&brcms_pci_driver);
  1118. }
  1119. module_init(brcms_module_init);
  1120. module_exit(brcms_module_exit);
  1121. /*
  1122. * precondition: perimeter lock has been acquired
  1123. */
  1124. void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
  1125. bool state, int prio)
  1126. {
  1127. wiphy_err(wl->wiphy, "Shouldn't be here %s\n", __func__);
  1128. }
  1129. /*
  1130. * precondition: perimeter lock has been acquired
  1131. */
  1132. void brcms_init(struct brcms_info *wl)
  1133. {
  1134. BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
  1135. brcms_reset(wl);
  1136. brcms_c_init(wl->wlc, wl->mute_tx);
  1137. }
  1138. /*
  1139. * precondition: perimeter lock has been acquired
  1140. */
  1141. uint brcms_reset(struct brcms_info *wl)
  1142. {
  1143. BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
  1144. brcms_c_reset(wl->wlc);
  1145. /* dpc will not be rescheduled */
  1146. wl->resched = 0;
  1147. return 0;
  1148. }
  1149. void brcms_fatal_error(struct brcms_info *wl)
  1150. {
  1151. wiphy_err(wl->wlc->wiphy, "wl%d: fatal error, reinitializing\n",
  1152. wl->wlc->pub->unit);
  1153. brcms_reset(wl);
  1154. ieee80211_restart_hw(wl->pub->ieee_hw);
  1155. }
  1156. /*
  1157. * These are interrupt on/off entry points. Disable interrupts
  1158. * during interrupt state transition.
  1159. */
  1160. void brcms_intrson(struct brcms_info *wl)
  1161. {
  1162. unsigned long flags;
  1163. spin_lock_irqsave(&wl->isr_lock, flags);
  1164. brcms_c_intrson(wl->wlc);
  1165. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1166. }
  1167. u32 brcms_intrsoff(struct brcms_info *wl)
  1168. {
  1169. unsigned long flags;
  1170. u32 status;
  1171. spin_lock_irqsave(&wl->isr_lock, flags);
  1172. status = brcms_c_intrsoff(wl->wlc);
  1173. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1174. return status;
  1175. }
  1176. void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
  1177. {
  1178. unsigned long flags;
  1179. spin_lock_irqsave(&wl->isr_lock, flags);
  1180. brcms_c_intrsrestore(wl->wlc, macintmask);
  1181. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1182. }
  1183. /*
  1184. * precondition: perimeter lock has been acquired
  1185. */
  1186. int brcms_up(struct brcms_info *wl)
  1187. {
  1188. int error = 0;
  1189. if (wl->pub->up)
  1190. return 0;
  1191. error = brcms_c_up(wl->wlc);
  1192. return error;
  1193. }
  1194. /*
  1195. * precondition: perimeter lock has been acquired
  1196. */
  1197. void brcms_down(struct brcms_info *wl)
  1198. {
  1199. uint callbacks, ret_val = 0;
  1200. /* call common down function */
  1201. ret_val = brcms_c_down(wl->wlc);
  1202. callbacks = atomic_read(&wl->callbacks) - ret_val;
  1203. /* wait for down callbacks to complete */
  1204. spin_unlock_bh(&wl->lock);
  1205. /* For HIGH_only driver, it's important to actually schedule other work,
  1206. * not just spin wait since everything runs at schedule level
  1207. */
  1208. SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
  1209. spin_lock_bh(&wl->lock);
  1210. }
  1211. /*
  1212. * precondition: perimeter lock is not acquired
  1213. */
  1214. static void _brcms_timer(struct work_struct *work)
  1215. {
  1216. struct brcms_timer *t = container_of(work, struct brcms_timer,
  1217. dly_wrk.work);
  1218. spin_lock_bh(&t->wl->lock);
  1219. if (t->set) {
  1220. if (t->periodic) {
  1221. atomic_inc(&t->wl->callbacks);
  1222. ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
  1223. &t->dly_wrk,
  1224. msecs_to_jiffies(t->ms));
  1225. } else {
  1226. t->set = false;
  1227. }
  1228. t->fn(t->arg);
  1229. }
  1230. atomic_dec(&t->wl->callbacks);
  1231. spin_unlock_bh(&t->wl->lock);
  1232. }
  1233. /*
  1234. * Adds a timer to the list. Caller supplies a timer function.
  1235. * Is called from wlc.
  1236. *
  1237. * precondition: perimeter lock has been acquired
  1238. */
  1239. struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
  1240. void (*fn) (void *arg),
  1241. void *arg, const char *name)
  1242. {
  1243. struct brcms_timer *t;
  1244. t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
  1245. if (!t)
  1246. return NULL;
  1247. INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
  1248. t->wl = wl;
  1249. t->fn = fn;
  1250. t->arg = arg;
  1251. t->next = wl->timers;
  1252. wl->timers = t;
  1253. #ifdef BCMDBG
  1254. t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
  1255. if (t->name)
  1256. strcpy(t->name, name);
  1257. #endif
  1258. return t;
  1259. }
  1260. /*
  1261. * adds only the kernel timer since it's going to be more accurate
  1262. * as well as it's easier to make it periodic
  1263. *
  1264. * precondition: perimeter lock has been acquired
  1265. */
  1266. void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
  1267. {
  1268. struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
  1269. #ifdef BCMDBG
  1270. if (t->set)
  1271. wiphy_err(hw->wiphy, "%s: Already set. Name: %s, per %d\n",
  1272. __func__, t->name, periodic);
  1273. #endif
  1274. t->ms = ms;
  1275. t->periodic = (bool) periodic;
  1276. t->set = true;
  1277. atomic_inc(&t->wl->callbacks);
  1278. ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
  1279. }
  1280. /*
  1281. * return true if timer successfully deleted, false if still pending
  1282. *
  1283. * precondition: perimeter lock has been acquired
  1284. */
  1285. bool brcms_del_timer(struct brcms_timer *t)
  1286. {
  1287. if (t->set) {
  1288. t->set = false;
  1289. if (!cancel_delayed_work(&t->dly_wrk))
  1290. return false;
  1291. atomic_dec(&t->wl->callbacks);
  1292. }
  1293. return true;
  1294. }
  1295. /*
  1296. * precondition: perimeter lock has been acquired
  1297. */
  1298. void brcms_free_timer(struct brcms_timer *t)
  1299. {
  1300. struct brcms_info *wl = t->wl;
  1301. struct brcms_timer *tmp;
  1302. /* delete the timer in case it is active */
  1303. brcms_del_timer(t);
  1304. if (wl->timers == t) {
  1305. wl->timers = wl->timers->next;
  1306. #ifdef BCMDBG
  1307. kfree(t->name);
  1308. #endif
  1309. kfree(t);
  1310. return;
  1311. }
  1312. tmp = wl->timers;
  1313. while (tmp) {
  1314. if (tmp->next == t) {
  1315. tmp->next = t->next;
  1316. #ifdef BCMDBG
  1317. kfree(t->name);
  1318. #endif
  1319. kfree(t);
  1320. return;
  1321. }
  1322. tmp = tmp->next;
  1323. }
  1324. }
  1325. /*
  1326. * precondition: perimeter lock has been acquired
  1327. */
  1328. int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
  1329. {
  1330. int i, entry;
  1331. const u8 *pdata;
  1332. struct firmware_hdr *hdr;
  1333. for (i = 0; i < wl->fw.fw_cnt; i++) {
  1334. hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
  1335. for (entry = 0; entry < wl->fw.hdr_num_entries[i];
  1336. entry++, hdr++) {
  1337. u32 len = le32_to_cpu(hdr->len);
  1338. if (le32_to_cpu(hdr->idx) == idx) {
  1339. pdata = wl->fw.fw_bin[i]->data +
  1340. le32_to_cpu(hdr->offset);
  1341. *pbuf = kmalloc(len, GFP_ATOMIC);
  1342. if (*pbuf == NULL)
  1343. goto fail;
  1344. memcpy(*pbuf, pdata, len);
  1345. return 0;
  1346. }
  1347. }
  1348. }
  1349. wiphy_err(wl->wiphy, "ERROR: ucode buf tag:%d can not be found!\n",
  1350. idx);
  1351. *pbuf = NULL;
  1352. fail:
  1353. return -ENODATA;
  1354. }
  1355. /*
  1356. * Precondition: Since this function is called in brcms_pci_probe() context,
  1357. * no locking is required.
  1358. */
  1359. int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
  1360. {
  1361. int i, entry;
  1362. const u8 *pdata;
  1363. struct firmware_hdr *hdr;
  1364. for (i = 0; i < wl->fw.fw_cnt; i++) {
  1365. hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
  1366. for (entry = 0; entry < wl->fw.hdr_num_entries[i];
  1367. entry++, hdr++) {
  1368. if (le32_to_cpu(hdr->idx) == idx) {
  1369. pdata = wl->fw.fw_bin[i]->data +
  1370. le32_to_cpu(hdr->offset);
  1371. if (le32_to_cpu(hdr->len) != 4) {
  1372. wiphy_err(wl->wiphy,
  1373. "ERROR: fw hdr len\n");
  1374. return -ENOMSG;
  1375. }
  1376. *n_bytes = le32_to_cpu(*((__le32 *) pdata));
  1377. return 0;
  1378. }
  1379. }
  1380. }
  1381. wiphy_err(wl->wiphy, "ERROR: ucode tag:%d can not be found!\n", idx);
  1382. return -ENOMSG;
  1383. }
  1384. /*
  1385. * precondition: can both be called locked and unlocked
  1386. */
  1387. void brcms_ucode_free_buf(void *p)
  1388. {
  1389. kfree(p);
  1390. }
  1391. /*
  1392. * checks validity of all firmware images loaded from user space
  1393. *
  1394. * Precondition: Since this function is called in brcms_pci_probe() context,
  1395. * no locking is required.
  1396. */
  1397. int brcms_check_firmwares(struct brcms_info *wl)
  1398. {
  1399. int i;
  1400. int entry;
  1401. int rc = 0;
  1402. const struct firmware *fw;
  1403. const struct firmware *fw_hdr;
  1404. struct firmware_hdr *ucode_hdr;
  1405. for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
  1406. fw = wl->fw.fw_bin[i];
  1407. fw_hdr = wl->fw.fw_hdr[i];
  1408. if (fw == NULL && fw_hdr == NULL) {
  1409. break;
  1410. } else if (fw == NULL || fw_hdr == NULL) {
  1411. wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
  1412. __func__);
  1413. rc = -EBADF;
  1414. } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
  1415. wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
  1416. "size %zu/%zu\n", __func__, fw_hdr->size,
  1417. sizeof(struct firmware_hdr));
  1418. rc = -EBADF;
  1419. } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
  1420. wiphy_err(wl->wiphy, "%s: out of bounds fw file size "
  1421. "%zu\n", __func__, fw->size);
  1422. rc = -EBADF;
  1423. } else {
  1424. /* check if ucode section overruns firmware image */
  1425. ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
  1426. for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
  1427. !rc; entry++, ucode_hdr++) {
  1428. if (le32_to_cpu(ucode_hdr->offset) +
  1429. le32_to_cpu(ucode_hdr->len) >
  1430. fw->size) {
  1431. wiphy_err(wl->wiphy,
  1432. "%s: conflicting bin/hdr\n",
  1433. __func__);
  1434. rc = -EBADF;
  1435. }
  1436. }
  1437. }
  1438. }
  1439. if (rc == 0 && wl->fw.fw_cnt != i) {
  1440. wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
  1441. wl->fw.fw_cnt);
  1442. rc = -EBADF;
  1443. }
  1444. return rc;
  1445. }
  1446. /*
  1447. * precondition: perimeter lock has been acquired
  1448. */
  1449. bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
  1450. {
  1451. bool blocked = brcms_c_check_radio_disabled(wl->wlc);
  1452. spin_unlock_bh(&wl->lock);
  1453. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
  1454. if (blocked)
  1455. wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
  1456. spin_lock_bh(&wl->lock);
  1457. return blocked;
  1458. }
  1459. /*
  1460. * precondition: perimeter lock has been acquired
  1461. */
  1462. void brcms_msleep(struct brcms_info *wl, uint ms)
  1463. {
  1464. spin_unlock_bh(&wl->lock);
  1465. msleep(ms);
  1466. spin_lock_bh(&wl->lock);
  1467. }