mac80211_if.c 40 KB

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