mac80211_if.c 40 KB

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