mac80211_if.c 40 KB

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