mac80211_if.c 42 KB

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