mac80211_if.c 42 KB

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