mac80211_if.c 42 KB

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