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