mac80211_if.c 42 KB

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