mac80211_if.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. 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. void brcms_dpc(unsigned long data)
  643. {
  644. struct brcms_info *wl;
  645. wl = (struct brcms_info *) data;
  646. spin_lock_bh(&wl->lock);
  647. /* call the common second level interrupt handler */
  648. if (wl->pub->up) {
  649. if (wl->resched) {
  650. unsigned long flags;
  651. spin_lock_irqsave(&wl->isr_lock, flags);
  652. brcms_c_intrsupd(wl->wlc);
  653. spin_unlock_irqrestore(&wl->isr_lock, flags);
  654. }
  655. wl->resched = brcms_c_dpc(wl->wlc, true);
  656. }
  657. /* brcms_c_dpc() may bring the driver down */
  658. if (!wl->pub->up)
  659. goto done;
  660. /* re-schedule dpc */
  661. if (wl->resched)
  662. tasklet_schedule(&wl->tasklet);
  663. else
  664. /* re-enable interrupts */
  665. brcms_intrson(wl);
  666. done:
  667. spin_unlock_bh(&wl->lock);
  668. }
  669. /*
  670. * Precondition: Since this function is called in brcms_pci_probe() context,
  671. * no locking is required.
  672. */
  673. static int brcms_request_fw(struct brcms_info *wl, struct bcma_device *pdev)
  674. {
  675. int status;
  676. struct device *device = &pdev->dev;
  677. char fw_name[100];
  678. int i;
  679. memset(&wl->fw, 0, sizeof(struct brcms_firmware));
  680. for (i = 0; i < MAX_FW_IMAGES; i++) {
  681. if (brcms_firmwares[i] == NULL)
  682. break;
  683. sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
  684. UCODE_LOADER_API_VER);
  685. status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
  686. if (status) {
  687. wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
  688. KBUILD_MODNAME, fw_name);
  689. return status;
  690. }
  691. sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
  692. UCODE_LOADER_API_VER);
  693. status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
  694. if (status) {
  695. wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
  696. KBUILD_MODNAME, fw_name);
  697. return status;
  698. }
  699. wl->fw.hdr_num_entries[i] =
  700. wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
  701. }
  702. wl->fw.fw_cnt = i;
  703. return brcms_ucode_data_init(wl, &wl->ucode);
  704. }
  705. /*
  706. * Precondition: Since this function is called in brcms_pci_probe() context,
  707. * no locking is required.
  708. */
  709. static void brcms_release_fw(struct brcms_info *wl)
  710. {
  711. int i;
  712. for (i = 0; i < MAX_FW_IMAGES; i++) {
  713. release_firmware(wl->fw.fw_bin[i]);
  714. release_firmware(wl->fw.fw_hdr[i]);
  715. }
  716. }
  717. /**
  718. * This function frees the WL per-device resources.
  719. *
  720. * This function frees resources owned by the WL device pointed to
  721. * by the wl parameter.
  722. *
  723. * precondition: can both be called locked and unlocked
  724. *
  725. */
  726. static void brcms_free(struct brcms_info *wl)
  727. {
  728. struct brcms_timer *t, *next;
  729. /* free ucode data */
  730. if (wl->fw.fw_cnt)
  731. brcms_ucode_data_free(&wl->ucode);
  732. if (wl->irq)
  733. free_irq(wl->irq, wl);
  734. /* kill dpc */
  735. tasklet_kill(&wl->tasklet);
  736. if (wl->pub)
  737. brcms_c_module_unregister(wl->pub, "linux", wl);
  738. /* free common resources */
  739. if (wl->wlc) {
  740. brcms_c_detach(wl->wlc);
  741. wl->wlc = NULL;
  742. wl->pub = NULL;
  743. }
  744. /* virtual interface deletion is deferred so we cannot spinwait */
  745. /* wait for all pending callbacks to complete */
  746. while (atomic_read(&wl->callbacks) > 0)
  747. schedule();
  748. /* free timers */
  749. for (t = wl->timers; t; t = next) {
  750. next = t->next;
  751. #ifdef DEBUG
  752. kfree(t->name);
  753. #endif
  754. kfree(t);
  755. }
  756. }
  757. /*
  758. * called from both kernel as from this kernel module (error flow on attach)
  759. * precondition: perimeter lock is not acquired.
  760. */
  761. static void brcms_remove(struct bcma_device *pdev)
  762. {
  763. struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
  764. struct brcms_info *wl = hw->priv;
  765. if (wl->wlc) {
  766. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
  767. wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
  768. ieee80211_unregister_hw(hw);
  769. }
  770. brcms_free(wl);
  771. bcma_set_drvdata(pdev, NULL);
  772. ieee80211_free_hw(hw);
  773. }
  774. static irqreturn_t brcms_isr(int irq, void *dev_id)
  775. {
  776. struct brcms_info *wl;
  777. bool ours, wantdpc;
  778. wl = (struct brcms_info *) dev_id;
  779. spin_lock(&wl->isr_lock);
  780. /* call common first level interrupt handler */
  781. ours = brcms_c_isr(wl->wlc, &wantdpc);
  782. if (ours) {
  783. /* if more to do... */
  784. if (wantdpc) {
  785. /* ...and call the second level interrupt handler */
  786. /* schedule dpc */
  787. tasklet_schedule(&wl->tasklet);
  788. }
  789. }
  790. spin_unlock(&wl->isr_lock);
  791. return IRQ_RETVAL(ours);
  792. }
  793. /*
  794. * is called in brcms_pci_probe() context, therefore no locking required.
  795. */
  796. static int ieee_hw_rate_init(struct ieee80211_hw *hw)
  797. {
  798. struct brcms_info *wl = hw->priv;
  799. struct brcms_c_info *wlc = wl->wlc;
  800. struct ieee80211_supported_band *band;
  801. int has_5g = 0;
  802. u16 phy_type;
  803. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  804. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  805. phy_type = brcms_c_get_phy_type(wl->wlc, 0);
  806. if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
  807. band = &wlc->bandstate[BAND_2G_INDEX]->band;
  808. *band = brcms_band_2GHz_nphy_template;
  809. if (phy_type == PHY_TYPE_LCN) {
  810. /* Single stream */
  811. band->ht_cap.mcs.rx_mask[1] = 0;
  812. band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
  813. }
  814. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  815. } else {
  816. return -EPERM;
  817. }
  818. /* Assume all bands use the same phy. True for 11n devices. */
  819. if (wl->pub->_nbands > 1) {
  820. has_5g++;
  821. if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
  822. band = &wlc->bandstate[BAND_5G_INDEX]->band;
  823. *band = brcms_band_5GHz_nphy_template;
  824. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  825. } else {
  826. return -EPERM;
  827. }
  828. }
  829. return 0;
  830. }
  831. /*
  832. * is called in brcms_pci_probe() context, therefore no locking required.
  833. */
  834. static int ieee_hw_init(struct ieee80211_hw *hw)
  835. {
  836. hw->flags = IEEE80211_HW_SIGNAL_DBM
  837. /* | IEEE80211_HW_CONNECTION_MONITOR What is this? */
  838. | IEEE80211_HW_REPORTS_TX_ACK_STATUS
  839. | IEEE80211_HW_AMPDU_AGGREGATION;
  840. hw->extra_tx_headroom = brcms_c_get_header_len();
  841. hw->queues = N_TX_QUEUES;
  842. hw->max_rates = 2; /* Primary rate and 1 fallback rate */
  843. /* channel change time is dependent on chip and band */
  844. hw->channel_change_time = 7 * 1000;
  845. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  846. hw->rate_control_algorithm = "minstrel_ht";
  847. hw->sta_data_size = 0;
  848. return ieee_hw_rate_init(hw);
  849. }
  850. /**
  851. * attach to the WL device.
  852. *
  853. * Attach to the WL device identified by vendor and device parameters.
  854. * regs is a host accessible memory address pointing to WL device registers.
  855. *
  856. * brcms_attach is not defined as static because in the case where no bus
  857. * is defined, wl_attach will never be called, and thus, gcc will issue
  858. * a warning that this function is defined but not used if we declare
  859. * it as static.
  860. *
  861. *
  862. * is called in brcms_bcma_probe() context, therefore no locking required.
  863. */
  864. static struct brcms_info *brcms_attach(struct bcma_device *pdev)
  865. {
  866. struct brcms_info *wl = NULL;
  867. int unit, err;
  868. struct ieee80211_hw *hw;
  869. u8 perm[ETH_ALEN];
  870. unit = n_adapters_found;
  871. err = 0;
  872. if (unit < 0)
  873. return NULL;
  874. /* allocate private info */
  875. hw = bcma_get_drvdata(pdev);
  876. if (hw != NULL)
  877. wl = hw->priv;
  878. if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
  879. return NULL;
  880. wl->wiphy = hw->wiphy;
  881. atomic_set(&wl->callbacks, 0);
  882. /* setup the bottom half handler */
  883. tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
  884. spin_lock_init(&wl->lock);
  885. spin_lock_init(&wl->isr_lock);
  886. /* prepare ucode */
  887. if (brcms_request_fw(wl, pdev) < 0) {
  888. wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
  889. "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
  890. brcms_release_fw(wl);
  891. brcms_remove(pdev);
  892. return NULL;
  893. }
  894. /* common load-time initialization */
  895. wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
  896. brcms_release_fw(wl);
  897. if (!wl->wlc) {
  898. wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
  899. KBUILD_MODNAME, err);
  900. goto fail;
  901. }
  902. wl->pub = brcms_c_pub(wl->wlc);
  903. wl->pub->ieee_hw = hw;
  904. /* register our interrupt handler */
  905. if (request_irq(pdev->irq, brcms_isr,
  906. IRQF_SHARED, KBUILD_MODNAME, wl)) {
  907. wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
  908. goto fail;
  909. }
  910. wl->irq = pdev->irq;
  911. /* register module */
  912. brcms_c_module_register(wl->pub, "linux", wl, NULL);
  913. if (ieee_hw_init(hw)) {
  914. wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
  915. __func__);
  916. goto fail;
  917. }
  918. memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
  919. if (WARN_ON(!is_valid_ether_addr(perm)))
  920. goto fail;
  921. SET_IEEE80211_PERM_ADDR(hw, perm);
  922. err = ieee80211_register_hw(hw);
  923. if (err)
  924. wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
  925. "%d\n", __func__, err);
  926. if (wl->pub->srom_ccode[0] &&
  927. regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
  928. wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
  929. n_adapters_found++;
  930. return wl;
  931. fail:
  932. brcms_free(wl);
  933. return NULL;
  934. }
  935. /**
  936. * determines if a device is a WL device, and if so, attaches it.
  937. *
  938. * This function determines if a device pointed to by pdev is a WL device,
  939. * and if so, performs a brcms_attach() on it.
  940. *
  941. * Perimeter lock is initialized in the course of this function.
  942. */
  943. static int __devinit brcms_bcma_probe(struct bcma_device *pdev)
  944. {
  945. struct brcms_info *wl;
  946. struct ieee80211_hw *hw;
  947. dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
  948. pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
  949. pdev->irq);
  950. if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
  951. (pdev->id.id != BCMA_CORE_80211))
  952. return -ENODEV;
  953. hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
  954. if (!hw) {
  955. pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
  956. return -ENOMEM;
  957. }
  958. SET_IEEE80211_DEV(hw, &pdev->dev);
  959. bcma_set_drvdata(pdev, hw);
  960. memset(hw->priv, 0, sizeof(*wl));
  961. wl = brcms_attach(pdev);
  962. if (!wl) {
  963. pr_err("%s: brcms_attach failed!\n", __func__);
  964. return -ENODEV;
  965. }
  966. return 0;
  967. }
  968. static int brcms_suspend(struct bcma_device *pdev)
  969. {
  970. struct brcms_info *wl;
  971. struct ieee80211_hw *hw;
  972. hw = bcma_get_drvdata(pdev);
  973. wl = hw->priv;
  974. if (!wl) {
  975. pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
  976. __func__);
  977. return -ENODEV;
  978. }
  979. /* only need to flag hw is down for proper resume */
  980. spin_lock_bh(&wl->lock);
  981. wl->pub->hw_up = false;
  982. spin_unlock_bh(&wl->lock);
  983. pr_debug("brcms_suspend ok\n");
  984. return 0;
  985. }
  986. static int brcms_resume(struct bcma_device *pdev)
  987. {
  988. pr_debug("brcms_resume ok\n");
  989. return 0;
  990. }
  991. static struct bcma_driver brcms_bcma_driver = {
  992. .name = KBUILD_MODNAME,
  993. .probe = brcms_bcma_probe,
  994. .suspend = brcms_suspend,
  995. .resume = brcms_resume,
  996. .remove = __devexit_p(brcms_remove),
  997. .id_table = brcms_coreid_table,
  998. };
  999. /**
  1000. * This is the main entry point for the brcmsmac driver.
  1001. *
  1002. * This function is scheduled upon module initialization and
  1003. * does the driver registration, which result in brcms_bcma_probe()
  1004. * call resulting in the driver bringup.
  1005. */
  1006. static void brcms_driver_init(struct work_struct *work)
  1007. {
  1008. int error;
  1009. error = bcma_driver_register(&brcms_bcma_driver);
  1010. if (error)
  1011. pr_err("%s: register returned %d\n", __func__, error);
  1012. }
  1013. static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
  1014. static int __init brcms_module_init(void)
  1015. {
  1016. #ifdef DEBUG
  1017. if (msglevel != 0xdeadbeef)
  1018. brcm_msg_level = msglevel;
  1019. #endif
  1020. if (!schedule_work(&brcms_driver_work))
  1021. return -EBUSY;
  1022. return 0;
  1023. }
  1024. /**
  1025. * This function unloads the brcmsmac driver from the system.
  1026. *
  1027. * This function unconditionally unloads the brcmsmac driver module from the
  1028. * system.
  1029. *
  1030. */
  1031. static void __exit brcms_module_exit(void)
  1032. {
  1033. cancel_work_sync(&brcms_driver_work);
  1034. bcma_driver_unregister(&brcms_bcma_driver);
  1035. }
  1036. module_init(brcms_module_init);
  1037. module_exit(brcms_module_exit);
  1038. /*
  1039. * precondition: perimeter lock has been acquired
  1040. */
  1041. void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
  1042. bool state, int prio)
  1043. {
  1044. wiphy_err(wl->wiphy, "Shouldn't be here %s\n", __func__);
  1045. }
  1046. /*
  1047. * precondition: perimeter lock has been acquired
  1048. */
  1049. void brcms_init(struct brcms_info *wl)
  1050. {
  1051. BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
  1052. brcms_reset(wl);
  1053. brcms_c_init(wl->wlc, wl->mute_tx);
  1054. }
  1055. /*
  1056. * precondition: perimeter lock has been acquired
  1057. */
  1058. uint brcms_reset(struct brcms_info *wl)
  1059. {
  1060. BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
  1061. brcms_c_reset(wl->wlc);
  1062. /* dpc will not be rescheduled */
  1063. wl->resched = false;
  1064. return 0;
  1065. }
  1066. void brcms_fatal_error(struct brcms_info *wl)
  1067. {
  1068. wiphy_err(wl->wlc->wiphy, "wl%d: fatal error, reinitializing\n",
  1069. wl->wlc->pub->unit);
  1070. brcms_reset(wl);
  1071. ieee80211_restart_hw(wl->pub->ieee_hw);
  1072. }
  1073. /*
  1074. * These are interrupt on/off entry points. Disable interrupts
  1075. * during interrupt state transition.
  1076. */
  1077. void brcms_intrson(struct brcms_info *wl)
  1078. {
  1079. unsigned long flags;
  1080. spin_lock_irqsave(&wl->isr_lock, flags);
  1081. brcms_c_intrson(wl->wlc);
  1082. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1083. }
  1084. u32 brcms_intrsoff(struct brcms_info *wl)
  1085. {
  1086. unsigned long flags;
  1087. u32 status;
  1088. spin_lock_irqsave(&wl->isr_lock, flags);
  1089. status = brcms_c_intrsoff(wl->wlc);
  1090. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1091. return status;
  1092. }
  1093. void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
  1094. {
  1095. unsigned long flags;
  1096. spin_lock_irqsave(&wl->isr_lock, flags);
  1097. brcms_c_intrsrestore(wl->wlc, macintmask);
  1098. spin_unlock_irqrestore(&wl->isr_lock, flags);
  1099. }
  1100. /*
  1101. * precondition: perimeter lock has been acquired
  1102. */
  1103. int brcms_up(struct brcms_info *wl)
  1104. {
  1105. int error = 0;
  1106. if (wl->pub->up)
  1107. return 0;
  1108. error = brcms_c_up(wl->wlc);
  1109. return error;
  1110. }
  1111. /*
  1112. * precondition: perimeter lock has been acquired
  1113. */
  1114. void brcms_down(struct brcms_info *wl)
  1115. {
  1116. uint callbacks, ret_val = 0;
  1117. /* call common down function */
  1118. ret_val = brcms_c_down(wl->wlc);
  1119. callbacks = atomic_read(&wl->callbacks) - ret_val;
  1120. /* wait for down callbacks to complete */
  1121. spin_unlock_bh(&wl->lock);
  1122. /* For HIGH_only driver, it's important to actually schedule other work,
  1123. * not just spin wait since everything runs at schedule level
  1124. */
  1125. SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
  1126. spin_lock_bh(&wl->lock);
  1127. }
  1128. /*
  1129. * precondition: perimeter lock is not acquired
  1130. */
  1131. static void _brcms_timer(struct work_struct *work)
  1132. {
  1133. struct brcms_timer *t = container_of(work, struct brcms_timer,
  1134. dly_wrk.work);
  1135. spin_lock_bh(&t->wl->lock);
  1136. if (t->set) {
  1137. if (t->periodic) {
  1138. atomic_inc(&t->wl->callbacks);
  1139. ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
  1140. &t->dly_wrk,
  1141. msecs_to_jiffies(t->ms));
  1142. } else {
  1143. t->set = false;
  1144. }
  1145. t->fn(t->arg);
  1146. }
  1147. atomic_dec(&t->wl->callbacks);
  1148. spin_unlock_bh(&t->wl->lock);
  1149. }
  1150. /*
  1151. * Adds a timer to the list. Caller supplies a timer function.
  1152. * Is called from wlc.
  1153. *
  1154. * precondition: perimeter lock has been acquired
  1155. */
  1156. struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
  1157. void (*fn) (void *arg),
  1158. void *arg, const char *name)
  1159. {
  1160. struct brcms_timer *t;
  1161. t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
  1162. if (!t)
  1163. return NULL;
  1164. INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
  1165. t->wl = wl;
  1166. t->fn = fn;
  1167. t->arg = arg;
  1168. t->next = wl->timers;
  1169. wl->timers = t;
  1170. #ifdef DEBUG
  1171. t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
  1172. if (t->name)
  1173. strcpy(t->name, name);
  1174. #endif
  1175. return t;
  1176. }
  1177. /*
  1178. * adds only the kernel timer since it's going to be more accurate
  1179. * as well as it's easier to make it periodic
  1180. *
  1181. * precondition: perimeter lock has been acquired
  1182. */
  1183. void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
  1184. {
  1185. struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
  1186. #ifdef DEBUG
  1187. if (t->set)
  1188. wiphy_err(hw->wiphy, "%s: Already set. Name: %s, per %d\n",
  1189. __func__, t->name, periodic);
  1190. #endif
  1191. t->ms = ms;
  1192. t->periodic = (bool) periodic;
  1193. t->set = true;
  1194. atomic_inc(&t->wl->callbacks);
  1195. ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
  1196. }
  1197. /*
  1198. * return true if timer successfully deleted, false if still pending
  1199. *
  1200. * precondition: perimeter lock has been acquired
  1201. */
  1202. bool brcms_del_timer(struct brcms_timer *t)
  1203. {
  1204. if (t->set) {
  1205. t->set = false;
  1206. if (!cancel_delayed_work(&t->dly_wrk))
  1207. return false;
  1208. atomic_dec(&t->wl->callbacks);
  1209. }
  1210. return true;
  1211. }
  1212. /*
  1213. * precondition: perimeter lock has been acquired
  1214. */
  1215. void brcms_free_timer(struct brcms_timer *t)
  1216. {
  1217. struct brcms_info *wl = t->wl;
  1218. struct brcms_timer *tmp;
  1219. /* delete the timer in case it is active */
  1220. brcms_del_timer(t);
  1221. if (wl->timers == t) {
  1222. wl->timers = wl->timers->next;
  1223. #ifdef DEBUG
  1224. kfree(t->name);
  1225. #endif
  1226. kfree(t);
  1227. return;
  1228. }
  1229. tmp = wl->timers;
  1230. while (tmp) {
  1231. if (tmp->next == t) {
  1232. tmp->next = t->next;
  1233. #ifdef DEBUG
  1234. kfree(t->name);
  1235. #endif
  1236. kfree(t);
  1237. return;
  1238. }
  1239. tmp = tmp->next;
  1240. }
  1241. }
  1242. /*
  1243. * precondition: perimeter lock has been acquired
  1244. */
  1245. int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
  1246. {
  1247. int i, entry;
  1248. const u8 *pdata;
  1249. struct firmware_hdr *hdr;
  1250. for (i = 0; i < wl->fw.fw_cnt; i++) {
  1251. hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
  1252. for (entry = 0; entry < wl->fw.hdr_num_entries[i];
  1253. entry++, hdr++) {
  1254. u32 len = le32_to_cpu(hdr->len);
  1255. if (le32_to_cpu(hdr->idx) == idx) {
  1256. pdata = wl->fw.fw_bin[i]->data +
  1257. le32_to_cpu(hdr->offset);
  1258. *pbuf = kmemdup(pdata, len, GFP_ATOMIC);
  1259. if (*pbuf == NULL)
  1260. goto fail;
  1261. return 0;
  1262. }
  1263. }
  1264. }
  1265. wiphy_err(wl->wiphy, "ERROR: ucode buf tag:%d can not be found!\n",
  1266. idx);
  1267. *pbuf = NULL;
  1268. fail:
  1269. return -ENODATA;
  1270. }
  1271. /*
  1272. * Precondition: Since this function is called in brcms_bcma_probe() context,
  1273. * no locking is required.
  1274. */
  1275. int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
  1276. {
  1277. int i, entry;
  1278. const u8 *pdata;
  1279. struct firmware_hdr *hdr;
  1280. for (i = 0; i < wl->fw.fw_cnt; i++) {
  1281. hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
  1282. for (entry = 0; entry < wl->fw.hdr_num_entries[i];
  1283. entry++, hdr++) {
  1284. if (le32_to_cpu(hdr->idx) == idx) {
  1285. pdata = wl->fw.fw_bin[i]->data +
  1286. le32_to_cpu(hdr->offset);
  1287. if (le32_to_cpu(hdr->len) != 4) {
  1288. wiphy_err(wl->wiphy,
  1289. "ERROR: fw hdr len\n");
  1290. return -ENOMSG;
  1291. }
  1292. *n_bytes = le32_to_cpu(*((__le32 *) pdata));
  1293. return 0;
  1294. }
  1295. }
  1296. }
  1297. wiphy_err(wl->wiphy, "ERROR: ucode tag:%d can not be found!\n", idx);
  1298. return -ENOMSG;
  1299. }
  1300. /*
  1301. * precondition: can both be called locked and unlocked
  1302. */
  1303. void brcms_ucode_free_buf(void *p)
  1304. {
  1305. kfree(p);
  1306. }
  1307. /*
  1308. * checks validity of all firmware images loaded from user space
  1309. *
  1310. * Precondition: Since this function is called in brcms_bcma_probe() context,
  1311. * no locking is required.
  1312. */
  1313. int brcms_check_firmwares(struct brcms_info *wl)
  1314. {
  1315. int i;
  1316. int entry;
  1317. int rc = 0;
  1318. const struct firmware *fw;
  1319. const struct firmware *fw_hdr;
  1320. struct firmware_hdr *ucode_hdr;
  1321. for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
  1322. fw = wl->fw.fw_bin[i];
  1323. fw_hdr = wl->fw.fw_hdr[i];
  1324. if (fw == NULL && fw_hdr == NULL) {
  1325. break;
  1326. } else if (fw == NULL || fw_hdr == NULL) {
  1327. wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
  1328. __func__);
  1329. rc = -EBADF;
  1330. } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
  1331. wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
  1332. "size %zu/%zu\n", __func__, fw_hdr->size,
  1333. sizeof(struct firmware_hdr));
  1334. rc = -EBADF;
  1335. } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
  1336. wiphy_err(wl->wiphy, "%s: out of bounds fw file size "
  1337. "%zu\n", __func__, fw->size);
  1338. rc = -EBADF;
  1339. } else {
  1340. /* check if ucode section overruns firmware image */
  1341. ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
  1342. for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
  1343. !rc; entry++, ucode_hdr++) {
  1344. if (le32_to_cpu(ucode_hdr->offset) +
  1345. le32_to_cpu(ucode_hdr->len) >
  1346. fw->size) {
  1347. wiphy_err(wl->wiphy,
  1348. "%s: conflicting bin/hdr\n",
  1349. __func__);
  1350. rc = -EBADF;
  1351. }
  1352. }
  1353. }
  1354. }
  1355. if (rc == 0 && wl->fw.fw_cnt != i) {
  1356. wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
  1357. wl->fw.fw_cnt);
  1358. rc = -EBADF;
  1359. }
  1360. return rc;
  1361. }
  1362. /*
  1363. * precondition: perimeter lock has been acquired
  1364. */
  1365. bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
  1366. {
  1367. bool blocked = brcms_c_check_radio_disabled(wl->wlc);
  1368. spin_unlock_bh(&wl->lock);
  1369. wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
  1370. if (blocked)
  1371. wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
  1372. spin_lock_bh(&wl->lock);
  1373. return blocked;
  1374. }
  1375. /*
  1376. * precondition: perimeter lock has been acquired
  1377. */
  1378. void brcms_msleep(struct brcms_info *wl, uint ms)
  1379. {
  1380. spin_unlock_bh(&wl->lock);
  1381. msleep(ms);
  1382. spin_lock_bh(&wl->lock);
  1383. }