mac80211_if.c 40 KB

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