rndis_wlan.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  1. /*
  2. * Driver for RNDIS based wireless USB devices.
  3. *
  4. * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
  5. * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Portions of this file are based on NDISwrapper project,
  22. * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
  23. * http://ndiswrapper.sourceforge.net/
  24. */
  25. // #define DEBUG // error path messages, extra info
  26. // #define VERBOSE // more; success messages
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/mutex.h>
  34. #include <linux/mii.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/cdc.h>
  37. #include <linux/wireless.h>
  38. #include <linux/ieee80211.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/ctype.h>
  41. #include <linux/spinlock.h>
  42. #include <net/iw_handler.h>
  43. #include <net/cfg80211.h>
  44. #include <linux/usb/usbnet.h>
  45. #include <linux/usb/rndis_host.h>
  46. /* NOTE: All these are settings for Broadcom chipset */
  47. static char modparam_country[4] = "EU";
  48. module_param_string(country, modparam_country, 4, 0444);
  49. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  50. static int modparam_frameburst = 1;
  51. module_param_named(frameburst, modparam_frameburst, int, 0444);
  52. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  53. static int modparam_afterburner = 0;
  54. module_param_named(afterburner, modparam_afterburner, int, 0444);
  55. MODULE_PARM_DESC(afterburner,
  56. "enable afterburner aka '125 High Speed Mode' (default: off)");
  57. static int modparam_power_save = 0;
  58. module_param_named(power_save, modparam_power_save, int, 0444);
  59. MODULE_PARM_DESC(power_save,
  60. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  61. static int modparam_power_output = 3;
  62. module_param_named(power_output, modparam_power_output, int, 0444);
  63. MODULE_PARM_DESC(power_output,
  64. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  65. static int modparam_roamtrigger = -70;
  66. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  67. MODULE_PARM_DESC(roamtrigger,
  68. "set roaming dBm trigger: -80=optimize for distance, "
  69. "-60=bandwidth (default: -70)");
  70. static int modparam_roamdelta = 1;
  71. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  72. MODULE_PARM_DESC(roamdelta,
  73. "set roaming tendency: 0=aggressive, 1=moderate, "
  74. "2=conservative (default: moderate)");
  75. static int modparam_workaround_interval = 500;
  76. module_param_named(workaround_interval, modparam_workaround_interval,
  77. int, 0444);
  78. MODULE_PARM_DESC(workaround_interval,
  79. "set stall workaround interval in msecs (default: 500)");
  80. /* various RNDIS OID defs */
  81. #define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
  82. #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
  83. #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
  84. #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
  85. #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
  86. #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
  87. #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
  88. #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
  89. #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
  90. #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
  91. #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
  92. #define OID_802_11_SSID cpu_to_le32(0x0d010102)
  93. #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
  94. #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
  95. #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
  96. #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
  97. #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
  98. #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
  99. #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
  100. #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
  101. #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
  102. #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
  103. #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
  104. #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
  105. #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
  106. #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
  107. #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
  108. #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
  109. #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
  110. #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
  111. #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
  112. #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
  113. #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
  114. #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
  115. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  116. #define WL_NOISE -96 /* typical noise level in dBm */
  117. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  118. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  119. * based on wireless rndis) has default txpower of 13dBm.
  120. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  121. * 100% : 20 mW ~ 13dBm
  122. * 75% : 15 mW ~ 12dBm
  123. * 50% : 10 mW ~ 10dBm
  124. * 25% : 5 mW ~ 7dBm
  125. */
  126. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  127. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  128. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  129. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  130. /* codes for "status" field of completion messages */
  131. #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
  132. #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
  133. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  134. * slightly modified for datatype endianess, etc
  135. */
  136. #define NDIS_802_11_LENGTH_SSID 32
  137. #define NDIS_802_11_LENGTH_RATES 8
  138. #define NDIS_802_11_LENGTH_RATES_EX 16
  139. enum ndis_80211_net_type {
  140. NDIS_80211_TYPE_FREQ_HOP,
  141. NDIS_80211_TYPE_DIRECT_SEQ,
  142. NDIS_80211_TYPE_OFDM_A,
  143. NDIS_80211_TYPE_OFDM_G
  144. };
  145. enum ndis_80211_net_infra {
  146. NDIS_80211_INFRA_ADHOC,
  147. NDIS_80211_INFRA_INFRA,
  148. NDIS_80211_INFRA_AUTO_UNKNOWN
  149. };
  150. enum ndis_80211_auth_mode {
  151. NDIS_80211_AUTH_OPEN,
  152. NDIS_80211_AUTH_SHARED,
  153. NDIS_80211_AUTH_AUTO_SWITCH,
  154. NDIS_80211_AUTH_WPA,
  155. NDIS_80211_AUTH_WPA_PSK,
  156. NDIS_80211_AUTH_WPA_NONE,
  157. NDIS_80211_AUTH_WPA2,
  158. NDIS_80211_AUTH_WPA2_PSK
  159. };
  160. enum ndis_80211_encr_status {
  161. NDIS_80211_ENCR_WEP_ENABLED,
  162. NDIS_80211_ENCR_DISABLED,
  163. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  164. NDIS_80211_ENCR_NOT_SUPPORTED,
  165. NDIS_80211_ENCR_TKIP_ENABLED,
  166. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  167. NDIS_80211_ENCR_CCMP_ENABLED,
  168. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  169. };
  170. enum ndis_80211_priv_filter {
  171. NDIS_80211_PRIV_ACCEPT_ALL,
  172. NDIS_80211_PRIV_8021X_WEP
  173. };
  174. enum ndis_80211_status_type {
  175. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  176. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  177. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  178. NDIS_80211_STATUSTYPE_RADIOSTATE,
  179. };
  180. enum ndis_80211_media_stream_mode {
  181. NDIS_80211_MEDIA_STREAM_OFF,
  182. NDIS_80211_MEDIA_STREAM_ON
  183. };
  184. enum ndis_80211_radio_status {
  185. NDIS_80211_RADIO_STATUS_ON,
  186. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  187. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  188. };
  189. enum ndis_80211_addkey_bits {
  190. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  191. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  192. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  193. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  194. };
  195. enum ndis_80211_addwep_bits {
  196. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  197. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  198. };
  199. struct ndis_80211_auth_request {
  200. __le32 length;
  201. u8 bssid[6];
  202. u8 padding[2];
  203. __le32 flags;
  204. } __attribute__((packed));
  205. struct ndis_80211_pmkid_candidate {
  206. u8 bssid[6];
  207. u8 padding[2];
  208. __le32 flags;
  209. } __attribute__((packed));
  210. struct ndis_80211_pmkid_cand_list {
  211. __le32 version;
  212. __le32 num_candidates;
  213. struct ndis_80211_pmkid_candidate candidate_list[0];
  214. } __attribute__((packed));
  215. struct ndis_80211_status_indication {
  216. __le32 status_type;
  217. union {
  218. __le32 media_stream_mode;
  219. __le32 radio_status;
  220. struct ndis_80211_auth_request auth_request[0];
  221. struct ndis_80211_pmkid_cand_list cand_list;
  222. } u;
  223. } __attribute__((packed));
  224. struct ndis_80211_ssid {
  225. __le32 length;
  226. u8 essid[NDIS_802_11_LENGTH_SSID];
  227. } __attribute__((packed));
  228. struct ndis_80211_conf_freq_hop {
  229. __le32 length;
  230. __le32 hop_pattern;
  231. __le32 hop_set;
  232. __le32 dwell_time;
  233. } __attribute__((packed));
  234. struct ndis_80211_conf {
  235. __le32 length;
  236. __le32 beacon_period;
  237. __le32 atim_window;
  238. __le32 ds_config;
  239. struct ndis_80211_conf_freq_hop fh_config;
  240. } __attribute__((packed));
  241. struct ndis_80211_bssid_ex {
  242. __le32 length;
  243. u8 mac[6];
  244. u8 padding[2];
  245. struct ndis_80211_ssid ssid;
  246. __le32 privacy;
  247. __le32 rssi;
  248. __le32 net_type;
  249. struct ndis_80211_conf config;
  250. __le32 net_infra;
  251. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  252. __le32 ie_length;
  253. u8 ies[0];
  254. } __attribute__((packed));
  255. struct ndis_80211_bssid_list_ex {
  256. __le32 num_items;
  257. struct ndis_80211_bssid_ex bssid[0];
  258. } __attribute__((packed));
  259. struct ndis_80211_fixed_ies {
  260. u8 timestamp[8];
  261. __le16 beacon_interval;
  262. __le16 capabilities;
  263. } __attribute__((packed));
  264. struct ndis_80211_wep_key {
  265. __le32 size;
  266. __le32 index;
  267. __le32 length;
  268. u8 material[32];
  269. } __attribute__((packed));
  270. struct ndis_80211_key {
  271. __le32 size;
  272. __le32 index;
  273. __le32 length;
  274. u8 bssid[6];
  275. u8 padding[6];
  276. u8 rsc[8];
  277. u8 material[32];
  278. } __attribute__((packed));
  279. struct ndis_80211_remove_key {
  280. __le32 size;
  281. __le32 index;
  282. u8 bssid[6];
  283. u8 padding[2];
  284. } __attribute__((packed));
  285. struct ndis_config_param {
  286. __le32 name_offs;
  287. __le32 name_length;
  288. __le32 type;
  289. __le32 value_offs;
  290. __le32 value_length;
  291. } __attribute__((packed));
  292. struct ndis_80211_assoc_info {
  293. __le32 length;
  294. __le16 req_ies;
  295. struct req_ie {
  296. __le16 capa;
  297. __le16 listen_interval;
  298. u8 cur_ap_address[6];
  299. } req_ie;
  300. __le32 req_ie_length;
  301. __le32 offset_req_ies;
  302. __le16 resp_ies;
  303. struct resp_ie {
  304. __le16 capa;
  305. __le16 status_code;
  306. __le16 assoc_id;
  307. } resp_ie;
  308. __le32 resp_ie_length;
  309. __le32 offset_resp_ies;
  310. } __attribute__((packed));
  311. /*
  312. * private data
  313. */
  314. #define NET_TYPE_11FB 0
  315. #define CAP_MODE_80211A 1
  316. #define CAP_MODE_80211B 2
  317. #define CAP_MODE_80211G 4
  318. #define CAP_MODE_MASK 7
  319. #define WORK_LINK_UP (1<<0)
  320. #define WORK_LINK_DOWN (1<<1)
  321. #define WORK_SET_MULTICAST_LIST (1<<2)
  322. #define RNDIS_WLAN_ALG_NONE 0
  323. #define RNDIS_WLAN_ALG_WEP (1<<0)
  324. #define RNDIS_WLAN_ALG_TKIP (1<<1)
  325. #define RNDIS_WLAN_ALG_CCMP (1<<2)
  326. #define RNDIS_WLAN_KEY_MGMT_NONE 0
  327. #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
  328. #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
  329. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  330. static const struct ieee80211_channel rndis_channels[] = {
  331. { .center_freq = 2412 },
  332. { .center_freq = 2417 },
  333. { .center_freq = 2422 },
  334. { .center_freq = 2427 },
  335. { .center_freq = 2432 },
  336. { .center_freq = 2437 },
  337. { .center_freq = 2442 },
  338. { .center_freq = 2447 },
  339. { .center_freq = 2452 },
  340. { .center_freq = 2457 },
  341. { .center_freq = 2462 },
  342. { .center_freq = 2467 },
  343. { .center_freq = 2472 },
  344. { .center_freq = 2484 },
  345. };
  346. static const struct ieee80211_rate rndis_rates[] = {
  347. { .bitrate = 10 },
  348. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  349. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  350. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  351. { .bitrate = 60 },
  352. { .bitrate = 90 },
  353. { .bitrate = 120 },
  354. { .bitrate = 180 },
  355. { .bitrate = 240 },
  356. { .bitrate = 360 },
  357. { .bitrate = 480 },
  358. { .bitrate = 540 }
  359. };
  360. static const u32 rndis_cipher_suites[] = {
  361. WLAN_CIPHER_SUITE_WEP40,
  362. WLAN_CIPHER_SUITE_WEP104,
  363. WLAN_CIPHER_SUITE_TKIP,
  364. WLAN_CIPHER_SUITE_CCMP,
  365. };
  366. struct rndis_wlan_encr_key {
  367. int len;
  368. u32 cipher;
  369. u8 material[32];
  370. u8 bssid[ETH_ALEN];
  371. bool pairwise;
  372. bool tx_key;
  373. };
  374. /* RNDIS device private data */
  375. struct rndis_wlan_private {
  376. struct usbnet *usbdev;
  377. struct wireless_dev wdev;
  378. struct cfg80211_scan_request *scan_request;
  379. struct workqueue_struct *workqueue;
  380. struct delayed_work dev_poller_work;
  381. struct delayed_work scan_work;
  382. struct work_struct work;
  383. struct mutex command_lock;
  384. unsigned long work_pending;
  385. int last_qual;
  386. struct ieee80211_supported_band band;
  387. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  388. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  389. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  390. int caps;
  391. int multicast_size;
  392. /* module parameters */
  393. char param_country[4];
  394. int param_frameburst;
  395. int param_afterburner;
  396. int param_power_save;
  397. int param_power_output;
  398. int param_roamtrigger;
  399. int param_roamdelta;
  400. u32 param_workaround_interval;
  401. /* hardware state */
  402. bool radio_on;
  403. int infra_mode;
  404. bool connected;
  405. u8 bssid[ETH_ALEN];
  406. struct ndis_80211_ssid essid;
  407. __le32 current_command_oid;
  408. /* encryption stuff */
  409. int encr_tx_key_index;
  410. struct rndis_wlan_encr_key encr_keys[4];
  411. enum nl80211_auth_type wpa_auth_type;
  412. int wpa_version;
  413. int wpa_keymgmt;
  414. int wpa_ie_len;
  415. u8 *wpa_ie;
  416. int wpa_cipher_pair;
  417. int wpa_cipher_group;
  418. u8 command_buffer[COMMAND_BUFFER_SIZE];
  419. };
  420. /*
  421. * cfg80211 ops
  422. */
  423. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  424. struct net_device *dev,
  425. enum nl80211_iftype type, u32 *flags,
  426. struct vif_params *params);
  427. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  428. struct cfg80211_scan_request *request);
  429. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  430. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  431. int dbm);
  432. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
  433. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  434. struct cfg80211_connect_params *sme);
  435. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  436. u16 reason_code);
  437. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  438. struct cfg80211_ibss_params *params);
  439. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
  440. static int rndis_set_channel(struct wiphy *wiphy,
  441. struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
  442. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  443. u8 key_index, const u8 *mac_addr,
  444. struct key_params *params);
  445. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  446. u8 key_index, const u8 *mac_addr);
  447. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  448. u8 key_index);
  449. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  450. u8 *mac, struct station_info *sinfo);
  451. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  452. int idx, u8 *mac, struct station_info *sinfo);
  453. static struct cfg80211_ops rndis_config_ops = {
  454. .change_virtual_intf = rndis_change_virtual_intf,
  455. .scan = rndis_scan,
  456. .set_wiphy_params = rndis_set_wiphy_params,
  457. .set_tx_power = rndis_set_tx_power,
  458. .get_tx_power = rndis_get_tx_power,
  459. .connect = rndis_connect,
  460. .disconnect = rndis_disconnect,
  461. .join_ibss = rndis_join_ibss,
  462. .leave_ibss = rndis_leave_ibss,
  463. .set_channel = rndis_set_channel,
  464. .add_key = rndis_add_key,
  465. .del_key = rndis_del_key,
  466. .set_default_key = rndis_set_default_key,
  467. .get_station = rndis_get_station,
  468. .dump_station = rndis_dump_station,
  469. };
  470. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  471. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  472. {
  473. return (struct rndis_wlan_private *)dev->driver_priv;
  474. }
  475. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  476. {
  477. switch (priv->param_power_output) {
  478. default:
  479. case 3:
  480. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  481. case 2:
  482. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  483. case 1:
  484. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  485. case 0:
  486. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  487. }
  488. }
  489. static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
  490. {
  491. int cipher = priv->encr_keys[idx].cipher;
  492. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  493. cipher == WLAN_CIPHER_SUITE_TKIP);
  494. }
  495. static int rndis_cipher_to_alg(u32 cipher)
  496. {
  497. switch (cipher) {
  498. default:
  499. return RNDIS_WLAN_ALG_NONE;
  500. case WLAN_CIPHER_SUITE_WEP40:
  501. case WLAN_CIPHER_SUITE_WEP104:
  502. return RNDIS_WLAN_ALG_WEP;
  503. case WLAN_CIPHER_SUITE_TKIP:
  504. return RNDIS_WLAN_ALG_TKIP;
  505. case WLAN_CIPHER_SUITE_CCMP:
  506. return RNDIS_WLAN_ALG_CCMP;
  507. }
  508. }
  509. static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
  510. {
  511. switch (akm_suite) {
  512. default:
  513. return RNDIS_WLAN_KEY_MGMT_NONE;
  514. case WLAN_AKM_SUITE_8021X:
  515. return RNDIS_WLAN_KEY_MGMT_802_1X;
  516. case WLAN_AKM_SUITE_PSK:
  517. return RNDIS_WLAN_KEY_MGMT_PSK;
  518. }
  519. }
  520. #ifdef DEBUG
  521. static const char *oid_to_string(__le32 oid)
  522. {
  523. switch (oid) {
  524. #define OID_STR(oid) case oid: return(#oid)
  525. /* from rndis_host.h */
  526. OID_STR(OID_802_3_PERMANENT_ADDRESS);
  527. OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
  528. OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
  529. OID_STR(OID_GEN_PHYSICAL_MEDIUM);
  530. /* from rndis_wlan.c */
  531. OID_STR(OID_GEN_LINK_SPEED);
  532. OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
  533. OID_STR(OID_GEN_XMIT_OK);
  534. OID_STR(OID_GEN_RCV_OK);
  535. OID_STR(OID_GEN_XMIT_ERROR);
  536. OID_STR(OID_GEN_RCV_ERROR);
  537. OID_STR(OID_GEN_RCV_NO_BUFFER);
  538. OID_STR(OID_802_3_CURRENT_ADDRESS);
  539. OID_STR(OID_802_3_MULTICAST_LIST);
  540. OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
  541. OID_STR(OID_802_11_BSSID);
  542. OID_STR(OID_802_11_SSID);
  543. OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
  544. OID_STR(OID_802_11_ADD_WEP);
  545. OID_STR(OID_802_11_REMOVE_WEP);
  546. OID_STR(OID_802_11_DISASSOCIATE);
  547. OID_STR(OID_802_11_AUTHENTICATION_MODE);
  548. OID_STR(OID_802_11_PRIVACY_FILTER);
  549. OID_STR(OID_802_11_BSSID_LIST_SCAN);
  550. OID_STR(OID_802_11_ENCRYPTION_STATUS);
  551. OID_STR(OID_802_11_ADD_KEY);
  552. OID_STR(OID_802_11_REMOVE_KEY);
  553. OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
  554. OID_STR(OID_802_11_PMKID);
  555. OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
  556. OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
  557. OID_STR(OID_802_11_TX_POWER_LEVEL);
  558. OID_STR(OID_802_11_RSSI);
  559. OID_STR(OID_802_11_RSSI_TRIGGER);
  560. OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
  561. OID_STR(OID_802_11_RTS_THRESHOLD);
  562. OID_STR(OID_802_11_SUPPORTED_RATES);
  563. OID_STR(OID_802_11_CONFIGURATION);
  564. OID_STR(OID_802_11_BSSID_LIST);
  565. #undef OID_STR
  566. }
  567. return "?";
  568. }
  569. #else
  570. static const char *oid_to_string(__le32 oid)
  571. {
  572. return "?";
  573. }
  574. #endif
  575. /* translate error code */
  576. static int rndis_error_status(__le32 rndis_status)
  577. {
  578. int ret = -EINVAL;
  579. switch (rndis_status) {
  580. case RNDIS_STATUS_SUCCESS:
  581. ret = 0;
  582. break;
  583. case RNDIS_STATUS_FAILURE:
  584. case RNDIS_STATUS_INVALID_DATA:
  585. ret = -EINVAL;
  586. break;
  587. case RNDIS_STATUS_NOT_SUPPORTED:
  588. ret = -EOPNOTSUPP;
  589. break;
  590. case RNDIS_STATUS_ADAPTER_NOT_READY:
  591. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  592. ret = -EBUSY;
  593. break;
  594. }
  595. return ret;
  596. }
  597. static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
  598. {
  599. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  600. union {
  601. void *buf;
  602. struct rndis_msg_hdr *header;
  603. struct rndis_query *get;
  604. struct rndis_query_c *get_c;
  605. } u;
  606. int ret, buflen;
  607. buflen = *len + sizeof(*u.get);
  608. if (buflen < CONTROL_BUFFER_SIZE)
  609. buflen = CONTROL_BUFFER_SIZE;
  610. if (buflen > COMMAND_BUFFER_SIZE) {
  611. u.buf = kmalloc(buflen, GFP_KERNEL);
  612. if (!u.buf)
  613. return -ENOMEM;
  614. } else {
  615. u.buf = priv->command_buffer;
  616. }
  617. mutex_lock(&priv->command_lock);
  618. memset(u.get, 0, sizeof *u.get);
  619. u.get->msg_type = RNDIS_MSG_QUERY;
  620. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  621. u.get->oid = oid;
  622. priv->current_command_oid = oid;
  623. ret = rndis_command(dev, u.header, buflen);
  624. priv->current_command_oid = 0;
  625. if (ret < 0)
  626. devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
  627. "(%08x)", oid_to_string(oid), ret,
  628. le32_to_cpu(u.get_c->status));
  629. if (ret == 0) {
  630. ret = le32_to_cpu(u.get_c->len);
  631. if (ret > *len)
  632. *len = ret;
  633. memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
  634. ret = rndis_error_status(u.get_c->status);
  635. if (ret < 0)
  636. devdbg(dev, "rndis_query_oid(%s): device returned "
  637. "error, 0x%08x (%d)", oid_to_string(oid),
  638. le32_to_cpu(u.get_c->status), ret);
  639. }
  640. mutex_unlock(&priv->command_lock);
  641. if (u.buf != priv->command_buffer)
  642. kfree(u.buf);
  643. return ret;
  644. }
  645. static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
  646. {
  647. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  648. union {
  649. void *buf;
  650. struct rndis_msg_hdr *header;
  651. struct rndis_set *set;
  652. struct rndis_set_c *set_c;
  653. } u;
  654. int ret, buflen;
  655. buflen = len + sizeof(*u.set);
  656. if (buflen < CONTROL_BUFFER_SIZE)
  657. buflen = CONTROL_BUFFER_SIZE;
  658. if (buflen > COMMAND_BUFFER_SIZE) {
  659. u.buf = kmalloc(buflen, GFP_KERNEL);
  660. if (!u.buf)
  661. return -ENOMEM;
  662. } else {
  663. u.buf = priv->command_buffer;
  664. }
  665. mutex_lock(&priv->command_lock);
  666. memset(u.set, 0, sizeof *u.set);
  667. u.set->msg_type = RNDIS_MSG_SET;
  668. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  669. u.set->oid = oid;
  670. u.set->len = cpu_to_le32(len);
  671. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  672. u.set->handle = cpu_to_le32(0);
  673. memcpy(u.buf + sizeof(*u.set), data, len);
  674. priv->current_command_oid = oid;
  675. ret = rndis_command(dev, u.header, buflen);
  676. priv->current_command_oid = 0;
  677. if (ret < 0)
  678. devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
  679. "(%08x)", oid_to_string(oid), ret,
  680. le32_to_cpu(u.set_c->status));
  681. if (ret == 0) {
  682. ret = rndis_error_status(u.set_c->status);
  683. if (ret < 0)
  684. devdbg(dev, "rndis_set_oid(%s): device returned error, "
  685. "0x%08x (%d)", oid_to_string(oid),
  686. le32_to_cpu(u.set_c->status), ret);
  687. }
  688. mutex_unlock(&priv->command_lock);
  689. if (u.buf != priv->command_buffer)
  690. kfree(u.buf);
  691. return ret;
  692. }
  693. static int rndis_reset(struct usbnet *usbdev)
  694. {
  695. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  696. struct rndis_reset *reset;
  697. int ret;
  698. mutex_lock(&priv->command_lock);
  699. reset = (void *)priv->command_buffer;
  700. memset(reset, 0, sizeof(*reset));
  701. reset->msg_type = RNDIS_MSG_RESET;
  702. reset->msg_len = cpu_to_le32(sizeof(*reset));
  703. priv->current_command_oid = 0;
  704. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  705. mutex_unlock(&priv->command_lock);
  706. if (ret < 0)
  707. return ret;
  708. return 0;
  709. }
  710. /*
  711. * Specs say that we can only set config parameters only soon after device
  712. * initialization.
  713. * value_type: 0 = u32, 2 = unicode string
  714. */
  715. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  716. int value_type, void *value)
  717. {
  718. struct ndis_config_param *infobuf;
  719. int value_len, info_len, param_len, ret, i;
  720. __le16 *unibuf;
  721. __le32 *dst_value;
  722. if (value_type == 0)
  723. value_len = sizeof(__le32);
  724. else if (value_type == 2)
  725. value_len = strlen(value) * sizeof(__le16);
  726. else
  727. return -EINVAL;
  728. param_len = strlen(param) * sizeof(__le16);
  729. info_len = sizeof(*infobuf) + param_len + value_len;
  730. #ifdef DEBUG
  731. info_len += 12;
  732. #endif
  733. infobuf = kmalloc(info_len, GFP_KERNEL);
  734. if (!infobuf)
  735. return -ENOMEM;
  736. #ifdef DEBUG
  737. info_len -= 12;
  738. /* extra 12 bytes are for padding (debug output) */
  739. memset(infobuf, 0xCC, info_len + 12);
  740. #endif
  741. if (value_type == 2)
  742. devdbg(dev, "setting config parameter: %s, value: %s",
  743. param, (u8 *)value);
  744. else
  745. devdbg(dev, "setting config parameter: %s, value: %d",
  746. param, *(u32 *)value);
  747. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  748. infobuf->name_length = cpu_to_le32(param_len);
  749. infobuf->type = cpu_to_le32(value_type);
  750. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  751. infobuf->value_length = cpu_to_le32(value_len);
  752. /* simple string to unicode string conversion */
  753. unibuf = (void *)infobuf + sizeof(*infobuf);
  754. for (i = 0; i < param_len / sizeof(__le16); i++)
  755. unibuf[i] = cpu_to_le16(param[i]);
  756. if (value_type == 2) {
  757. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  758. for (i = 0; i < value_len / sizeof(__le16); i++)
  759. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  760. } else {
  761. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  762. *dst_value = cpu_to_le32(*(u32 *)value);
  763. }
  764. #ifdef DEBUG
  765. devdbg(dev, "info buffer (len: %d):", info_len);
  766. for (i = 0; i < info_len; i += 12) {
  767. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  768. devdbg(dev, "%08X:%08X:%08X",
  769. cpu_to_be32(tmp[0]),
  770. cpu_to_be32(tmp[1]),
  771. cpu_to_be32(tmp[2]));
  772. }
  773. #endif
  774. ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
  775. infobuf, info_len);
  776. if (ret != 0)
  777. devdbg(dev, "setting rndis config parameter failed, %d.", ret);
  778. kfree(infobuf);
  779. return ret;
  780. }
  781. static int rndis_set_config_parameter_str(struct usbnet *dev,
  782. char *param, char *value)
  783. {
  784. return rndis_set_config_parameter(dev, param, 2, value);
  785. }
  786. /*
  787. * data conversion functions
  788. */
  789. static int level_to_qual(int level)
  790. {
  791. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  792. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  793. }
  794. /*
  795. * common functions
  796. */
  797. static int set_infra_mode(struct usbnet *usbdev, int mode);
  798. static void restore_keys(struct usbnet *usbdev);
  799. static int rndis_check_bssid_list(struct usbnet *usbdev);
  800. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  801. {
  802. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  803. int ret;
  804. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  805. if (ret < 0) {
  806. devwarn(usbdev, "setting SSID failed (%08X)", ret);
  807. return ret;
  808. }
  809. if (ret == 0) {
  810. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  811. priv->radio_on = true;
  812. devdbg(usbdev, "set_essid: radio_on = true");
  813. }
  814. return ret;
  815. }
  816. static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  817. {
  818. int ret;
  819. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  820. if (ret < 0) {
  821. devwarn(usbdev, "setting BSSID[%pM] failed (%08X)", bssid, ret);
  822. return ret;
  823. }
  824. return ret;
  825. }
  826. static int clear_bssid(struct usbnet *usbdev)
  827. {
  828. u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  829. return set_bssid(usbdev, broadcast_mac);
  830. }
  831. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  832. {
  833. int ret, len;
  834. len = ETH_ALEN;
  835. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  836. if (ret != 0)
  837. memset(bssid, 0, ETH_ALEN);
  838. return ret;
  839. }
  840. static int get_association_info(struct usbnet *usbdev,
  841. struct ndis_80211_assoc_info *info, int len)
  842. {
  843. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  844. info, &len);
  845. }
  846. static bool is_associated(struct usbnet *usbdev)
  847. {
  848. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  849. u8 bssid[ETH_ALEN];
  850. int ret;
  851. if (!priv->radio_on)
  852. return false;
  853. ret = get_bssid(usbdev, bssid);
  854. return (ret == 0 && !is_zero_ether_addr(bssid));
  855. }
  856. static int disassociate(struct usbnet *usbdev, bool reset_ssid)
  857. {
  858. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  859. struct ndis_80211_ssid ssid;
  860. int i, ret = 0;
  861. if (priv->radio_on) {
  862. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  863. if (ret == 0) {
  864. priv->radio_on = false;
  865. devdbg(usbdev, "disassociate: radio_on = false");
  866. if (reset_ssid)
  867. msleep(100);
  868. }
  869. }
  870. /* disassociate causes radio to be turned off; if reset_ssid
  871. * is given, set random ssid to enable radio */
  872. if (reset_ssid) {
  873. /* Set device to infrastructure mode so we don't get ad-hoc
  874. * 'media connect' indications with the random ssid.
  875. */
  876. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  877. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  878. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  879. ssid.essid[0] = 0x1;
  880. ssid.essid[1] = 0xff;
  881. for (i = 2; i < sizeof(ssid.essid); i++)
  882. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  883. ret = set_essid(usbdev, &ssid);
  884. }
  885. return ret;
  886. }
  887. static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
  888. enum nl80211_auth_type auth_type, int keymgmt)
  889. {
  890. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  891. __le32 tmp;
  892. int auth_mode, ret;
  893. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  894. "keymgmt=0x%x", wpa_version, auth_type, keymgmt);
  895. if (wpa_version & NL80211_WPA_VERSION_2) {
  896. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  897. auth_mode = NDIS_80211_AUTH_WPA2;
  898. else
  899. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  900. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  901. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  902. auth_mode = NDIS_80211_AUTH_WPA;
  903. else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
  904. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  905. else
  906. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  907. } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  908. auth_mode = NDIS_80211_AUTH_SHARED;
  909. else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
  910. auth_mode = NDIS_80211_AUTH_OPEN;
  911. else
  912. return -ENOTSUPP;
  913. tmp = cpu_to_le32(auth_mode);
  914. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  915. sizeof(tmp));
  916. if (ret != 0) {
  917. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  918. return ret;
  919. }
  920. priv->wpa_version = wpa_version;
  921. priv->wpa_auth_type = auth_type;
  922. priv->wpa_keymgmt = keymgmt;
  923. return 0;
  924. }
  925. static int set_priv_filter(struct usbnet *usbdev)
  926. {
  927. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  928. __le32 tmp;
  929. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  930. if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
  931. priv->wpa_version & NL80211_WPA_VERSION_1)
  932. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  933. else
  934. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  935. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  936. sizeof(tmp));
  937. }
  938. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  939. {
  940. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  941. __le32 tmp;
  942. int encr_mode, ret;
  943. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  944. pairwise, groupwise);
  945. if (pairwise & RNDIS_WLAN_ALG_CCMP)
  946. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  947. else if (pairwise & RNDIS_WLAN_ALG_TKIP)
  948. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  949. else if (pairwise & RNDIS_WLAN_ALG_WEP)
  950. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  951. else if (groupwise & RNDIS_WLAN_ALG_CCMP)
  952. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  953. else if (groupwise & RNDIS_WLAN_ALG_TKIP)
  954. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  955. else
  956. encr_mode = NDIS_80211_ENCR_DISABLED;
  957. tmp = cpu_to_le32(encr_mode);
  958. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  959. sizeof(tmp));
  960. if (ret != 0) {
  961. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  962. return ret;
  963. }
  964. priv->wpa_cipher_pair = pairwise;
  965. priv->wpa_cipher_group = groupwise;
  966. return 0;
  967. }
  968. static int set_infra_mode(struct usbnet *usbdev, int mode)
  969. {
  970. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  971. __le32 tmp;
  972. int ret;
  973. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  974. tmp = cpu_to_le32(mode);
  975. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  976. sizeof(tmp));
  977. if (ret != 0) {
  978. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  979. return ret;
  980. }
  981. /* NDIS drivers clear keys when infrastructure mode is
  982. * changed. But Linux tools assume otherwise. So set the
  983. * keys */
  984. restore_keys(usbdev);
  985. priv->infra_mode = mode;
  986. return 0;
  987. }
  988. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  989. {
  990. __le32 tmp;
  991. devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
  992. if (rts_threshold < 0 || rts_threshold > 2347)
  993. rts_threshold = 2347;
  994. tmp = cpu_to_le32(rts_threshold);
  995. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  996. sizeof(tmp));
  997. }
  998. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  999. {
  1000. __le32 tmp;
  1001. devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
  1002. if (frag_threshold < 256 || frag_threshold > 2346)
  1003. frag_threshold = 2346;
  1004. tmp = cpu_to_le32(frag_threshold);
  1005. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  1006. sizeof(tmp));
  1007. }
  1008. static void set_default_iw_params(struct usbnet *usbdev)
  1009. {
  1010. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1011. set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
  1012. RNDIS_WLAN_KEY_MGMT_NONE);
  1013. set_priv_filter(usbdev);
  1014. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1015. }
  1016. static int deauthenticate(struct usbnet *usbdev)
  1017. {
  1018. int ret;
  1019. ret = disassociate(usbdev, true);
  1020. set_default_iw_params(usbdev);
  1021. return ret;
  1022. }
  1023. static int set_channel(struct usbnet *usbdev, int channel)
  1024. {
  1025. struct ndis_80211_conf config;
  1026. unsigned int dsconfig;
  1027. int len, ret;
  1028. devdbg(usbdev, "set_channel(%d)", channel);
  1029. /* this OID is valid only when not associated */
  1030. if (is_associated(usbdev))
  1031. return 0;
  1032. dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
  1033. len = sizeof(config);
  1034. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1035. if (ret < 0) {
  1036. devdbg(usbdev, "set_channel: querying configuration failed");
  1037. return ret;
  1038. }
  1039. config.ds_config = cpu_to_le32(dsconfig);
  1040. ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1041. sizeof(config));
  1042. devdbg(usbdev, "set_channel: %d -> %d", channel, ret);
  1043. return ret;
  1044. }
  1045. /* index must be 0 - N, as per NDIS */
  1046. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1047. int index)
  1048. {
  1049. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1050. struct ndis_80211_wep_key ndis_key;
  1051. u32 cipher;
  1052. int ret;
  1053. devdbg(usbdev, "add_wep_key(idx: %d, len: %d)", index, key_len);
  1054. if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
  1055. return -EINVAL;
  1056. if (key_len == 5)
  1057. cipher = WLAN_CIPHER_SUITE_WEP40;
  1058. else
  1059. cipher = WLAN_CIPHER_SUITE_WEP104;
  1060. memset(&ndis_key, 0, sizeof(ndis_key));
  1061. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1062. ndis_key.length = cpu_to_le32(key_len);
  1063. ndis_key.index = cpu_to_le32(index);
  1064. memcpy(&ndis_key.material, key, key_len);
  1065. if (index == priv->encr_tx_key_index) {
  1066. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1067. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1068. RNDIS_WLAN_ALG_NONE);
  1069. if (ret)
  1070. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  1071. ret);
  1072. }
  1073. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1074. sizeof(ndis_key));
  1075. if (ret != 0) {
  1076. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  1077. index+1, ret);
  1078. return ret;
  1079. }
  1080. priv->encr_keys[index].len = key_len;
  1081. priv->encr_keys[index].cipher = cipher;
  1082. memcpy(&priv->encr_keys[index].material, key, key_len);
  1083. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1084. return 0;
  1085. }
  1086. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1087. int index, const u8 *addr, const u8 *rx_seq,
  1088. int seq_len, u32 cipher, __le32 flags)
  1089. {
  1090. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1091. struct ndis_80211_key ndis_key;
  1092. bool is_addr_ok;
  1093. int ret;
  1094. if (index < 0 || index >= 4) {
  1095. devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
  1096. return -EINVAL;
  1097. }
  1098. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1099. devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
  1100. key_len);
  1101. return -EINVAL;
  1102. }
  1103. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1104. if (!rx_seq || seq_len <= 0) {
  1105. devdbg(usbdev, "add_wpa_key: recv seq flag without"
  1106. "buffer");
  1107. return -EINVAL;
  1108. }
  1109. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1110. devdbg(usbdev, "add_wpa_key: too big recv seq buffer");
  1111. return -EINVAL;
  1112. }
  1113. }
  1114. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1115. !is_broadcast_ether_addr(addr);
  1116. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1117. devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
  1118. addr);
  1119. return -EINVAL;
  1120. }
  1121. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  1122. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1123. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1124. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1125. memset(&ndis_key, 0, sizeof(ndis_key));
  1126. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1127. sizeof(ndis_key.material) + key_len);
  1128. ndis_key.length = cpu_to_le32(key_len);
  1129. ndis_key.index = cpu_to_le32(index) | flags;
  1130. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1131. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1132. * different order than NDIS spec, so swap the order here. */
  1133. memcpy(ndis_key.material, key, 16);
  1134. memcpy(ndis_key.material + 16, key + 24, 8);
  1135. memcpy(ndis_key.material + 24, key + 16, 8);
  1136. } else
  1137. memcpy(ndis_key.material, key, key_len);
  1138. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1139. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1140. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1141. /* pairwise key */
  1142. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1143. } else {
  1144. /* group key */
  1145. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1146. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1147. else
  1148. get_bssid(usbdev, ndis_key.bssid);
  1149. }
  1150. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1151. le32_to_cpu(ndis_key.size));
  1152. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  1153. if (ret != 0)
  1154. return ret;
  1155. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1156. priv->encr_keys[index].len = key_len;
  1157. priv->encr_keys[index].cipher = cipher;
  1158. memcpy(&priv->encr_keys[index].material, key, key_len);
  1159. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1160. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1161. else
  1162. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1163. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1164. priv->encr_tx_key_index = index;
  1165. return 0;
  1166. }
  1167. static int restore_key(struct usbnet *usbdev, int key_idx)
  1168. {
  1169. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1170. struct rndis_wlan_encr_key key;
  1171. if (is_wpa_key(priv, key_idx))
  1172. return 0;
  1173. key = priv->encr_keys[key_idx];
  1174. devdbg(usbdev, "restore_key: %i:%i", key_idx, key.len);
  1175. if (key.len == 0)
  1176. return 0;
  1177. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1178. }
  1179. static void restore_keys(struct usbnet *usbdev)
  1180. {
  1181. int i;
  1182. for (i = 0; i < 4; i++)
  1183. restore_key(usbdev, i);
  1184. }
  1185. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1186. {
  1187. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1188. }
  1189. /* remove_key is for both wep and wpa */
  1190. static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
  1191. {
  1192. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1193. struct ndis_80211_remove_key remove_key;
  1194. __le32 keyindex;
  1195. bool is_wpa;
  1196. int ret;
  1197. if (priv->encr_keys[index].len == 0)
  1198. return 0;
  1199. is_wpa = is_wpa_key(priv, index);
  1200. devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
  1201. priv->encr_keys[index].len);
  1202. clear_key(priv, index);
  1203. if (is_wpa) {
  1204. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1205. remove_key.index = cpu_to_le32(index);
  1206. if (bssid) {
  1207. /* pairwise key */
  1208. if (!is_broadcast_ether_addr(bssid))
  1209. remove_key.index |=
  1210. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1211. memcpy(remove_key.bssid, bssid,
  1212. sizeof(remove_key.bssid));
  1213. } else
  1214. memset(remove_key.bssid, 0xff,
  1215. sizeof(remove_key.bssid));
  1216. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1217. sizeof(remove_key));
  1218. if (ret != 0)
  1219. return ret;
  1220. } else {
  1221. keyindex = cpu_to_le32(index);
  1222. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1223. sizeof(keyindex));
  1224. if (ret != 0) {
  1225. devwarn(usbdev,
  1226. "removing encryption key %d failed (%08X)",
  1227. index, ret);
  1228. return ret;
  1229. }
  1230. }
  1231. /* if it is transmit key, disable encryption */
  1232. if (index == priv->encr_tx_key_index)
  1233. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1234. return 0;
  1235. }
  1236. static void set_multicast_list(struct usbnet *usbdev)
  1237. {
  1238. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1239. struct dev_mc_list *mclist;
  1240. __le32 filter;
  1241. int ret, i, size;
  1242. char *buf;
  1243. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1244. if (usbdev->net->flags & IFF_PROMISC) {
  1245. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1246. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1247. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  1248. usbdev->net->mc_count > priv->multicast_size) {
  1249. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1250. } else if (usbdev->net->mc_count > 0) {
  1251. size = min(priv->multicast_size, usbdev->net->mc_count);
  1252. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  1253. if (!buf) {
  1254. devwarn(usbdev,
  1255. "couldn't alloc %d bytes of memory",
  1256. size * ETH_ALEN);
  1257. return;
  1258. }
  1259. mclist = usbdev->net->mc_list;
  1260. for (i = 0; i < size && mclist; mclist = mclist->next) {
  1261. if (mclist->dmi_addrlen != ETH_ALEN)
  1262. continue;
  1263. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  1264. i++;
  1265. }
  1266. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  1267. i * ETH_ALEN);
  1268. if (ret == 0 && i > 0)
  1269. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1270. else
  1271. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1272. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  1273. i, priv->multicast_size, ret);
  1274. kfree(buf);
  1275. }
  1276. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1277. sizeof(filter));
  1278. if (ret < 0) {
  1279. devwarn(usbdev, "couldn't set packet filter: %08x",
  1280. le32_to_cpu(filter));
  1281. }
  1282. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1283. le32_to_cpu(filter), ret);
  1284. }
  1285. /*
  1286. * cfg80211 ops
  1287. */
  1288. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1289. struct net_device *dev,
  1290. enum nl80211_iftype type, u32 *flags,
  1291. struct vif_params *params)
  1292. {
  1293. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1294. struct usbnet *usbdev = priv->usbdev;
  1295. int mode;
  1296. switch (type) {
  1297. case NL80211_IFTYPE_ADHOC:
  1298. mode = NDIS_80211_INFRA_ADHOC;
  1299. break;
  1300. case NL80211_IFTYPE_STATION:
  1301. mode = NDIS_80211_INFRA_INFRA;
  1302. break;
  1303. default:
  1304. return -EINVAL;
  1305. }
  1306. priv->wdev.iftype = type;
  1307. return set_infra_mode(usbdev, mode);
  1308. }
  1309. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1310. {
  1311. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1312. struct usbnet *usbdev = priv->usbdev;
  1313. int err;
  1314. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1315. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1316. if (err < 0)
  1317. return err;
  1318. }
  1319. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1320. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1321. if (err < 0)
  1322. return err;
  1323. }
  1324. return 0;
  1325. }
  1326. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1327. int dbm)
  1328. {
  1329. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1330. struct usbnet *usbdev = priv->usbdev;
  1331. devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
  1332. /* Device doesn't support changing txpower after initialization, only
  1333. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1334. * currently used.
  1335. */
  1336. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1337. if (!priv->radio_on)
  1338. disassociate(usbdev, true); /* turn on radio */
  1339. return 0;
  1340. }
  1341. return -ENOTSUPP;
  1342. }
  1343. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1344. {
  1345. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1346. struct usbnet *usbdev = priv->usbdev;
  1347. *dbm = get_bcm4320_power_dbm(priv);
  1348. devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
  1349. return 0;
  1350. }
  1351. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1352. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1353. struct cfg80211_scan_request *request)
  1354. {
  1355. struct usbnet *usbdev = netdev_priv(dev);
  1356. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1357. int ret;
  1358. __le32 tmp;
  1359. devdbg(usbdev, "cfg80211.scan");
  1360. /* Get current bssid list from device before new scan, as new scan
  1361. * clears internal bssid list.
  1362. */
  1363. rndis_check_bssid_list(usbdev);
  1364. if (!request)
  1365. return -EINVAL;
  1366. if (priv->scan_request && priv->scan_request != request)
  1367. return -EBUSY;
  1368. priv->scan_request = request;
  1369. tmp = cpu_to_le32(1);
  1370. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1371. sizeof(tmp));
  1372. if (ret == 0) {
  1373. /* Wait before retrieving scan results from device */
  1374. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1375. SCAN_DELAY_JIFFIES);
  1376. }
  1377. return ret;
  1378. }
  1379. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1380. struct ndis_80211_bssid_ex *bssid)
  1381. {
  1382. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1383. struct ieee80211_channel *channel;
  1384. s32 signal;
  1385. u64 timestamp;
  1386. u16 capability;
  1387. u16 beacon_interval;
  1388. struct ndis_80211_fixed_ies *fixed;
  1389. int ie_len, bssid_len;
  1390. u8 *ie;
  1391. devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
  1392. bssid->mac);
  1393. /* parse bssid structure */
  1394. bssid_len = le32_to_cpu(bssid->length);
  1395. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1396. sizeof(struct ndis_80211_fixed_ies))
  1397. return NULL;
  1398. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1399. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1400. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1401. (int)le32_to_cpu(bssid->ie_length));
  1402. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1403. if (ie_len < 0)
  1404. return NULL;
  1405. /* extract data for cfg80211_inform_bss */
  1406. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1407. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1408. if (!channel)
  1409. return NULL;
  1410. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1411. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1412. capability = le16_to_cpu(fixed->capabilities);
  1413. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1414. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1415. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1416. GFP_KERNEL);
  1417. }
  1418. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1419. {
  1420. void *buf = NULL;
  1421. struct ndis_80211_bssid_list_ex *bssid_list;
  1422. struct ndis_80211_bssid_ex *bssid;
  1423. int ret = -EINVAL, len, count, bssid_len;
  1424. bool resized = false;
  1425. devdbg(usbdev, "check_bssid_list");
  1426. len = CONTROL_BUFFER_SIZE;
  1427. resize_buf:
  1428. buf = kmalloc(len, GFP_KERNEL);
  1429. if (!buf) {
  1430. ret = -ENOMEM;
  1431. goto out;
  1432. }
  1433. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1434. if (ret != 0)
  1435. goto out;
  1436. if (!resized && len > CONTROL_BUFFER_SIZE) {
  1437. resized = true;
  1438. kfree(buf);
  1439. goto resize_buf;
  1440. }
  1441. bssid_list = buf;
  1442. bssid = bssid_list->bssid;
  1443. bssid_len = le32_to_cpu(bssid->length);
  1444. count = le32_to_cpu(bssid_list->num_items);
  1445. devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
  1446. len);
  1447. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1448. rndis_bss_info_update(usbdev, bssid);
  1449. bssid = (void *)bssid + bssid_len;
  1450. bssid_len = le32_to_cpu(bssid->length);
  1451. count--;
  1452. }
  1453. out:
  1454. kfree(buf);
  1455. return ret;
  1456. }
  1457. static void rndis_get_scan_results(struct work_struct *work)
  1458. {
  1459. struct rndis_wlan_private *priv =
  1460. container_of(work, struct rndis_wlan_private, scan_work.work);
  1461. struct usbnet *usbdev = priv->usbdev;
  1462. int ret;
  1463. devdbg(usbdev, "get_scan_results");
  1464. if (!priv->scan_request)
  1465. return;
  1466. ret = rndis_check_bssid_list(usbdev);
  1467. cfg80211_scan_done(priv->scan_request, ret < 0);
  1468. priv->scan_request = NULL;
  1469. }
  1470. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  1471. struct cfg80211_connect_params *sme)
  1472. {
  1473. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1474. struct usbnet *usbdev = priv->usbdev;
  1475. struct ieee80211_channel *channel = sme->channel;
  1476. struct ndis_80211_ssid ssid;
  1477. int pairwise = RNDIS_WLAN_ALG_NONE;
  1478. int groupwise = RNDIS_WLAN_ALG_NONE;
  1479. int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
  1480. int length, i, ret, chan = -1;
  1481. if (channel)
  1482. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1483. groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
  1484. for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
  1485. pairwise |=
  1486. rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
  1487. if (sme->crypto.n_ciphers_pairwise > 0 &&
  1488. pairwise == RNDIS_WLAN_ALG_NONE) {
  1489. deverr(usbdev, "Unsupported pairwise cipher");
  1490. return -ENOTSUPP;
  1491. }
  1492. for (i = 0; i < sme->crypto.n_akm_suites; i++)
  1493. keymgmt |=
  1494. rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
  1495. if (sme->crypto.n_akm_suites > 0 &&
  1496. keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
  1497. deverr(usbdev, "Invalid keymgmt");
  1498. return -ENOTSUPP;
  1499. }
  1500. devdbg(usbdev, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:"
  1501. "0x%x]:0x%x)", sme->ssid, sme->bssid, chan,
  1502. sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
  1503. groupwise, pairwise, keymgmt);
  1504. if (is_associated(usbdev))
  1505. disassociate(usbdev, false);
  1506. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1507. if (ret < 0) {
  1508. devdbg(usbdev, "connect: set_infra_mode failed, %d", ret);
  1509. goto err_turn_radio_on;
  1510. }
  1511. ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
  1512. keymgmt);
  1513. if (ret < 0) {
  1514. devdbg(usbdev, "connect: set_auth_mode failed, %d", ret);
  1515. goto err_turn_radio_on;
  1516. }
  1517. set_priv_filter(usbdev);
  1518. ret = set_encr_mode(usbdev, pairwise, groupwise);
  1519. if (ret < 0) {
  1520. devdbg(usbdev, "connect: set_encr_mode failed, %d", ret);
  1521. goto err_turn_radio_on;
  1522. }
  1523. if (channel) {
  1524. ret = set_channel(usbdev, chan);
  1525. if (ret < 0) {
  1526. devdbg(usbdev, "connect: set_channel failed, %d", ret);
  1527. goto err_turn_radio_on;
  1528. }
  1529. }
  1530. if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
  1531. priv->encr_tx_key_index = sme->key_idx;
  1532. ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
  1533. if (ret < 0) {
  1534. devdbg(usbdev, "connect: add_wep_key failed, %d "
  1535. "(%d, %d)", ret, sme->key_len, sme->key_idx);
  1536. goto err_turn_radio_on;
  1537. }
  1538. }
  1539. if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
  1540. !is_broadcast_ether_addr(sme->bssid)) {
  1541. ret = set_bssid(usbdev, sme->bssid);
  1542. if (ret < 0) {
  1543. devdbg(usbdev, "connect: set_bssid failed, %d", ret);
  1544. goto err_turn_radio_on;
  1545. }
  1546. } else
  1547. clear_bssid(usbdev);
  1548. length = sme->ssid_len;
  1549. if (length > NDIS_802_11_LENGTH_SSID)
  1550. length = NDIS_802_11_LENGTH_SSID;
  1551. memset(&ssid, 0, sizeof(ssid));
  1552. ssid.length = cpu_to_le32(length);
  1553. memcpy(ssid.essid, sme->ssid, length);
  1554. /* Pause and purge rx queue, so we don't pass packets before
  1555. * 'media connect'-indication.
  1556. */
  1557. usbnet_pause_rx(usbdev);
  1558. usbnet_purge_paused_rxq(usbdev);
  1559. ret = set_essid(usbdev, &ssid);
  1560. if (ret < 0)
  1561. devdbg(usbdev, "connect: set_essid failed, %d", ret);
  1562. return ret;
  1563. err_turn_radio_on:
  1564. disassociate(usbdev, true);
  1565. return ret;
  1566. }
  1567. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1568. u16 reason_code)
  1569. {
  1570. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1571. struct usbnet *usbdev = priv->usbdev;
  1572. devdbg(usbdev, "cfg80211.disconnect(%d)", reason_code);
  1573. priv->connected = false;
  1574. memset(priv->bssid, 0, ETH_ALEN);
  1575. return deauthenticate(usbdev);
  1576. }
  1577. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1578. struct cfg80211_ibss_params *params)
  1579. {
  1580. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1581. struct usbnet *usbdev = priv->usbdev;
  1582. struct ieee80211_channel *channel = params->channel;
  1583. struct ndis_80211_ssid ssid;
  1584. enum nl80211_auth_type auth_type;
  1585. int ret, alg, length, chan = -1;
  1586. if (channel)
  1587. chan = ieee80211_frequency_to_channel(channel->center_freq);
  1588. /* TODO: How to handle ad-hoc encryption?
  1589. * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
  1590. * pre-shared for encryption (open/shared/wpa), is key set before
  1591. * join_ibss? Which auth_type to use (not in params)? What about WPA?
  1592. */
  1593. if (params->privacy) {
  1594. auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  1595. alg = RNDIS_WLAN_ALG_WEP;
  1596. } else {
  1597. auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  1598. alg = RNDIS_WLAN_ALG_NONE;
  1599. }
  1600. devdbg(usbdev, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)", params->ssid,
  1601. params->bssid, chan, params->privacy);
  1602. if (is_associated(usbdev))
  1603. disassociate(usbdev, false);
  1604. ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
  1605. if (ret < 0) {
  1606. devdbg(usbdev, "join_ibss: set_infra_mode failed, %d", ret);
  1607. goto err_turn_radio_on;
  1608. }
  1609. ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
  1610. if (ret < 0) {
  1611. devdbg(usbdev, "join_ibss: set_auth_mode failed, %d", ret);
  1612. goto err_turn_radio_on;
  1613. }
  1614. set_priv_filter(usbdev);
  1615. ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
  1616. if (ret < 0) {
  1617. devdbg(usbdev, "join_ibss: set_encr_mode failed, %d", ret);
  1618. goto err_turn_radio_on;
  1619. }
  1620. if (channel) {
  1621. ret = set_channel(usbdev, chan);
  1622. if (ret < 0) {
  1623. devdbg(usbdev, "join_ibss: set_channel failed, %d",
  1624. ret);
  1625. goto err_turn_radio_on;
  1626. }
  1627. }
  1628. if (params->bssid && !is_zero_ether_addr(params->bssid) &&
  1629. !is_broadcast_ether_addr(params->bssid)) {
  1630. ret = set_bssid(usbdev, params->bssid);
  1631. if (ret < 0) {
  1632. devdbg(usbdev, "join_ibss: set_bssid failed, %d", ret);
  1633. goto err_turn_radio_on;
  1634. }
  1635. } else
  1636. clear_bssid(usbdev);
  1637. length = params->ssid_len;
  1638. if (length > NDIS_802_11_LENGTH_SSID)
  1639. length = NDIS_802_11_LENGTH_SSID;
  1640. memset(&ssid, 0, sizeof(ssid));
  1641. ssid.length = cpu_to_le32(length);
  1642. memcpy(ssid.essid, params->ssid, length);
  1643. /* Don't need to pause rx queue for ad-hoc. */
  1644. usbnet_purge_paused_rxq(usbdev);
  1645. usbnet_resume_rx(usbdev);
  1646. ret = set_essid(usbdev, &ssid);
  1647. if (ret < 0)
  1648. devdbg(usbdev, "join_ibss: set_essid failed, %d", ret);
  1649. return ret;
  1650. err_turn_radio_on:
  1651. disassociate(usbdev, true);
  1652. return ret;
  1653. }
  1654. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1655. {
  1656. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1657. struct usbnet *usbdev = priv->usbdev;
  1658. devdbg(usbdev, "cfg80211.leave_ibss()");
  1659. priv->connected = false;
  1660. memset(priv->bssid, 0, ETH_ALEN);
  1661. return deauthenticate(usbdev);
  1662. }
  1663. static int rndis_set_channel(struct wiphy *wiphy,
  1664. struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
  1665. {
  1666. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1667. struct usbnet *usbdev = priv->usbdev;
  1668. return set_channel(usbdev,
  1669. ieee80211_frequency_to_channel(chan->center_freq));
  1670. }
  1671. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1672. u8 key_index, const u8 *mac_addr,
  1673. struct key_params *params)
  1674. {
  1675. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1676. struct usbnet *usbdev = priv->usbdev;
  1677. __le32 flags;
  1678. devdbg(usbdev, "rndis_add_key(%i, %pM, %08x)", key_index, mac_addr,
  1679. params->cipher);
  1680. switch (params->cipher) {
  1681. case WLAN_CIPHER_SUITE_WEP40:
  1682. case WLAN_CIPHER_SUITE_WEP104:
  1683. return add_wep_key(usbdev, params->key, params->key_len,
  1684. key_index);
  1685. case WLAN_CIPHER_SUITE_TKIP:
  1686. case WLAN_CIPHER_SUITE_CCMP:
  1687. flags = 0;
  1688. if (params->seq && params->seq_len > 0)
  1689. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1690. if (mac_addr)
  1691. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
  1692. NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1693. return add_wpa_key(usbdev, params->key, params->key_len,
  1694. key_index, mac_addr, params->seq,
  1695. params->seq_len, params->cipher, flags);
  1696. default:
  1697. devdbg(usbdev, "rndis_add_key: unsupported cipher %08x",
  1698. params->cipher);
  1699. return -ENOTSUPP;
  1700. }
  1701. }
  1702. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1703. u8 key_index, const u8 *mac_addr)
  1704. {
  1705. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1706. struct usbnet *usbdev = priv->usbdev;
  1707. devdbg(usbdev, "rndis_del_key(%i, %pM)", key_index, mac_addr);
  1708. return remove_key(usbdev, key_index, mac_addr);
  1709. }
  1710. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  1711. u8 key_index)
  1712. {
  1713. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1714. struct usbnet *usbdev = priv->usbdev;
  1715. struct rndis_wlan_encr_key key;
  1716. devdbg(usbdev, "rndis_set_default_key(%i)", key_index);
  1717. priv->encr_tx_key_index = key_index;
  1718. key = priv->encr_keys[key_index];
  1719. return add_wep_key(usbdev, key.material, key.len, key_index);
  1720. }
  1721. static void rndis_fill_station_info(struct usbnet *usbdev,
  1722. struct station_info *sinfo)
  1723. {
  1724. __le32 linkspeed, rssi;
  1725. int ret, len;
  1726. memset(sinfo, 0, sizeof(*sinfo));
  1727. len = sizeof(linkspeed);
  1728. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
  1729. if (ret == 0) {
  1730. sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
  1731. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1732. }
  1733. len = sizeof(rssi);
  1734. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  1735. if (ret == 0) {
  1736. sinfo->signal = level_to_qual(le32_to_cpu(rssi));
  1737. sinfo->filled |= STATION_INFO_SIGNAL;
  1738. }
  1739. }
  1740. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  1741. u8 *mac, struct station_info *sinfo)
  1742. {
  1743. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1744. struct usbnet *usbdev = priv->usbdev;
  1745. if (compare_ether_addr(priv->bssid, mac))
  1746. return -ENOENT;
  1747. rndis_fill_station_info(usbdev, sinfo);
  1748. return 0;
  1749. }
  1750. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  1751. int idx, u8 *mac, struct station_info *sinfo)
  1752. {
  1753. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1754. struct usbnet *usbdev = priv->usbdev;
  1755. if (idx != 0)
  1756. return -ENOENT;
  1757. memcpy(mac, priv->bssid, ETH_ALEN);
  1758. rndis_fill_station_info(usbdev, sinfo);
  1759. return 0;
  1760. }
  1761. /*
  1762. * workers, indication handlers, device poller
  1763. */
  1764. static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
  1765. {
  1766. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1767. struct ndis_80211_assoc_info *info;
  1768. u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
  1769. u8 bssid[ETH_ALEN];
  1770. int resp_ie_len, req_ie_len;
  1771. u8 *req_ie, *resp_ie;
  1772. int ret, offset;
  1773. bool roamed = false;
  1774. if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
  1775. /* received media connect indication while connected, either
  1776. * device reassociated with same AP or roamed to new. */
  1777. roamed = true;
  1778. }
  1779. req_ie_len = 0;
  1780. resp_ie_len = 0;
  1781. req_ie = NULL;
  1782. resp_ie = NULL;
  1783. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  1784. memset(assoc_buf, 0, sizeof(assoc_buf));
  1785. info = (void *)assoc_buf;
  1786. /* Get association info IEs from device and send them back to
  1787. * userspace. */
  1788. ret = get_association_info(usbdev, info, sizeof(assoc_buf));
  1789. if (!ret) {
  1790. req_ie_len = le32_to_cpu(info->req_ie_length);
  1791. if (req_ie_len > 0) {
  1792. offset = le32_to_cpu(info->offset_req_ies);
  1793. req_ie = (u8 *)info + offset;
  1794. }
  1795. resp_ie_len = le32_to_cpu(info->resp_ie_length);
  1796. if (resp_ie_len > 0) {
  1797. offset = le32_to_cpu(info->offset_resp_ies);
  1798. resp_ie = (u8 *)info + offset;
  1799. }
  1800. }
  1801. } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
  1802. return;
  1803. ret = get_bssid(usbdev, bssid);
  1804. if (ret < 0)
  1805. memset(bssid, 0, sizeof(bssid));
  1806. devdbg(usbdev, "link up work: [%pM] %s", bssid, roamed ? "roamed" : "");
  1807. /* Internal bss list in device always contains at least the currently
  1808. * connected bss and we can get it to cfg80211 with
  1809. * rndis_check_bssid_list().
  1810. * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
  1811. * spec.
  1812. */
  1813. rndis_check_bssid_list(usbdev);
  1814. if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
  1815. if (!roamed)
  1816. cfg80211_connect_result(usbdev->net, bssid, req_ie,
  1817. req_ie_len, resp_ie,
  1818. resp_ie_len, 0, GFP_KERNEL);
  1819. else
  1820. cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
  1821. resp_ie, resp_ie_len, GFP_KERNEL);
  1822. } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1823. cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
  1824. priv->connected = true;
  1825. memcpy(priv->bssid, bssid, ETH_ALEN);
  1826. usbnet_resume_rx(usbdev);
  1827. netif_carrier_on(usbdev->net);
  1828. }
  1829. static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
  1830. {
  1831. union iwreq_data evt;
  1832. netif_carrier_off(usbdev->net);
  1833. evt.data.flags = 0;
  1834. evt.data.length = 0;
  1835. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1836. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1837. }
  1838. static void rndis_wlan_worker(struct work_struct *work)
  1839. {
  1840. struct rndis_wlan_private *priv =
  1841. container_of(work, struct rndis_wlan_private, work);
  1842. struct usbnet *usbdev = priv->usbdev;
  1843. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
  1844. rndis_wlan_do_link_up_work(usbdev);
  1845. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
  1846. rndis_wlan_do_link_down_work(usbdev);
  1847. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1848. set_multicast_list(usbdev);
  1849. }
  1850. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1851. {
  1852. struct usbnet *usbdev = netdev_priv(dev);
  1853. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1854. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1855. return;
  1856. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1857. queue_work(priv->workqueue, &priv->work);
  1858. }
  1859. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1860. struct ndis_80211_status_indication *indication,
  1861. int len)
  1862. {
  1863. u8 *buf;
  1864. const char *type;
  1865. int flags, buflen, key_id;
  1866. bool pairwise_error, group_error;
  1867. struct ndis_80211_auth_request *auth_req;
  1868. enum nl80211_key_type key_type;
  1869. /* must have at least one array entry */
  1870. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1871. sizeof(struct ndis_80211_auth_request)) {
  1872. devinfo(usbdev, "authentication indication: "
  1873. "too short message (%i)", len);
  1874. return;
  1875. }
  1876. buf = (void *)&indication->u.auth_request[0];
  1877. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1878. while (buflen >= sizeof(*auth_req)) {
  1879. auth_req = (void *)buf;
  1880. type = "unknown";
  1881. flags = le32_to_cpu(auth_req->flags);
  1882. pairwise_error = false;
  1883. group_error = false;
  1884. if (flags & 0x1)
  1885. type = "reauth request";
  1886. if (flags & 0x2)
  1887. type = "key update request";
  1888. if (flags & 0x6) {
  1889. pairwise_error = true;
  1890. type = "pairwise_error";
  1891. }
  1892. if (flags & 0xe) {
  1893. group_error = true;
  1894. type = "group_error";
  1895. }
  1896. devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
  1897. le32_to_cpu(auth_req->flags));
  1898. if (pairwise_error) {
  1899. key_type = NL80211_KEYTYPE_PAIRWISE;
  1900. key_id = -1;
  1901. cfg80211_michael_mic_failure(usbdev->net,
  1902. auth_req->bssid,
  1903. key_type, key_id, NULL,
  1904. GFP_KERNEL);
  1905. }
  1906. if (group_error) {
  1907. key_type = NL80211_KEYTYPE_GROUP;
  1908. key_id = -1;
  1909. cfg80211_michael_mic_failure(usbdev->net,
  1910. auth_req->bssid,
  1911. key_type, key_id, NULL,
  1912. GFP_KERNEL);
  1913. }
  1914. buflen -= le32_to_cpu(auth_req->length);
  1915. buf += le32_to_cpu(auth_req->length);
  1916. }
  1917. }
  1918. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  1919. struct ndis_80211_status_indication *indication,
  1920. int len)
  1921. {
  1922. struct ndis_80211_pmkid_cand_list *cand_list;
  1923. int list_len, expected_len, i;
  1924. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1925. sizeof(struct ndis_80211_pmkid_cand_list)) {
  1926. devinfo(usbdev, "pmkid candidate list indication: "
  1927. "too short message (%i)", len);
  1928. return;
  1929. }
  1930. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  1931. sizeof(struct ndis_80211_pmkid_candidate);
  1932. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  1933. offsetof(struct ndis_80211_status_indication, u);
  1934. if (len < expected_len) {
  1935. devinfo(usbdev, "pmkid candidate list indication: "
  1936. "list larger than buffer (%i < %i)",
  1937. len, expected_len);
  1938. return;
  1939. }
  1940. cand_list = &indication->u.cand_list;
  1941. devinfo(usbdev, "pmkid candidate list indication: "
  1942. "version %i, candidates %i",
  1943. le32_to_cpu(cand_list->version),
  1944. le32_to_cpu(cand_list->num_candidates));
  1945. if (le32_to_cpu(cand_list->version) != 1)
  1946. return;
  1947. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  1948. struct ndis_80211_pmkid_candidate *cand =
  1949. &cand_list->candidate_list[i];
  1950. devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
  1951. i, le32_to_cpu(cand->flags), cand->bssid);
  1952. #if 0
  1953. struct iw_pmkid_cand pcand;
  1954. union iwreq_data wrqu;
  1955. memset(&pcand, 0, sizeof(pcand));
  1956. if (le32_to_cpu(cand->flags) & 0x01)
  1957. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  1958. pcand.index = i;
  1959. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  1960. memset(&wrqu, 0, sizeof(wrqu));
  1961. wrqu.data.length = sizeof(pcand);
  1962. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  1963. (u8 *)&pcand);
  1964. #endif
  1965. }
  1966. }
  1967. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  1968. struct rndis_indicate *msg, int buflen)
  1969. {
  1970. struct ndis_80211_status_indication *indication;
  1971. int len, offset;
  1972. offset = offsetof(struct rndis_indicate, status) +
  1973. le32_to_cpu(msg->offset);
  1974. len = le32_to_cpu(msg->length);
  1975. if (len < 8) {
  1976. devinfo(usbdev, "media specific indication, "
  1977. "ignore too short message (%i < 8)", len);
  1978. return;
  1979. }
  1980. if (offset + len > buflen) {
  1981. devinfo(usbdev, "media specific indication, "
  1982. "too large to fit to buffer (%i > %i)",
  1983. offset + len, buflen);
  1984. return;
  1985. }
  1986. indication = (void *)((u8 *)msg + offset);
  1987. switch (le32_to_cpu(indication->status_type)) {
  1988. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  1989. devinfo(usbdev, "radio state indication: %i",
  1990. le32_to_cpu(indication->u.radio_status));
  1991. return;
  1992. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  1993. devinfo(usbdev, "media stream mode indication: %i",
  1994. le32_to_cpu(indication->u.media_stream_mode));
  1995. return;
  1996. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  1997. rndis_wlan_auth_indication(usbdev, indication, len);
  1998. return;
  1999. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2000. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2001. return;
  2002. default:
  2003. devinfo(usbdev, "media specific indication: "
  2004. "unknown status type 0x%08x",
  2005. le32_to_cpu(indication->status_type));
  2006. }
  2007. }
  2008. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2009. {
  2010. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2011. struct rndis_indicate *msg = ind;
  2012. switch (msg->status) {
  2013. case RNDIS_STATUS_MEDIA_CONNECT:
  2014. if (priv->current_command_oid == OID_802_11_ADD_KEY) {
  2015. /* OID_802_11_ADD_KEY causes sometimes extra
  2016. * "media connect" indications which confuses driver
  2017. * and userspace to think that device is
  2018. * roaming/reassociating when it isn't.
  2019. */
  2020. devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
  2021. "'media connect'");
  2022. return;
  2023. }
  2024. usbnet_pause_rx(usbdev);
  2025. devinfo(usbdev, "media connect");
  2026. /* queue work to avoid recursive calls into rndis_command */
  2027. set_bit(WORK_LINK_UP, &priv->work_pending);
  2028. queue_work(priv->workqueue, &priv->work);
  2029. break;
  2030. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2031. devinfo(usbdev, "media disconnect");
  2032. /* queue work to avoid recursive calls into rndis_command */
  2033. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2034. queue_work(priv->workqueue, &priv->work);
  2035. break;
  2036. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2037. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2038. break;
  2039. default:
  2040. devinfo(usbdev, "indication: 0x%08x",
  2041. le32_to_cpu(msg->status));
  2042. break;
  2043. }
  2044. }
  2045. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2046. {
  2047. struct {
  2048. __le32 num_items;
  2049. __le32 items[8];
  2050. } networks_supported;
  2051. int len, retval, i, n;
  2052. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2053. /* determine supported modes */
  2054. len = sizeof(networks_supported);
  2055. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2056. &networks_supported, &len);
  2057. if (retval >= 0) {
  2058. n = le32_to_cpu(networks_supported.num_items);
  2059. if (n > 8)
  2060. n = 8;
  2061. for (i = 0; i < n; i++) {
  2062. switch (le32_to_cpu(networks_supported.items[i])) {
  2063. case NDIS_80211_TYPE_FREQ_HOP:
  2064. case NDIS_80211_TYPE_DIRECT_SEQ:
  2065. priv->caps |= CAP_MODE_80211B;
  2066. break;
  2067. case NDIS_80211_TYPE_OFDM_A:
  2068. priv->caps |= CAP_MODE_80211A;
  2069. break;
  2070. case NDIS_80211_TYPE_OFDM_G:
  2071. priv->caps |= CAP_MODE_80211G;
  2072. break;
  2073. }
  2074. }
  2075. }
  2076. return retval;
  2077. }
  2078. #define DEVICE_POLLER_JIFFIES (HZ)
  2079. static void rndis_device_poller(struct work_struct *work)
  2080. {
  2081. struct rndis_wlan_private *priv =
  2082. container_of(work, struct rndis_wlan_private,
  2083. dev_poller_work.work);
  2084. struct usbnet *usbdev = priv->usbdev;
  2085. __le32 rssi, tmp;
  2086. int len, ret, j;
  2087. int update_jiffies = DEVICE_POLLER_JIFFIES;
  2088. void *buf;
  2089. /* Only check/do workaround when connected. Calling is_associated()
  2090. * also polls device with rndis_command() and catches for media link
  2091. * indications.
  2092. */
  2093. if (!is_associated(usbdev))
  2094. goto end;
  2095. len = sizeof(rssi);
  2096. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2097. if (ret == 0)
  2098. priv->last_qual = level_to_qual(le32_to_cpu(rssi));
  2099. devdbg(usbdev, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d",
  2100. ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
  2101. /* Workaround transfer stalls on poor quality links.
  2102. * TODO: find right way to fix these stalls (as stalls do not happen
  2103. * with ndiswrapper/windows driver). */
  2104. if (priv->last_qual <= 25) {
  2105. /* Decrease stats worker interval to catch stalls.
  2106. * faster. Faster than 400-500ms causes packet loss,
  2107. * Slower doesn't catch stalls fast enough.
  2108. */
  2109. j = msecs_to_jiffies(priv->param_workaround_interval);
  2110. if (j > DEVICE_POLLER_JIFFIES)
  2111. j = DEVICE_POLLER_JIFFIES;
  2112. else if (j <= 0)
  2113. j = 1;
  2114. update_jiffies = j;
  2115. /* Send scan OID. Use of both OIDs is required to get device
  2116. * working.
  2117. */
  2118. tmp = cpu_to_le32(1);
  2119. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2120. sizeof(tmp));
  2121. len = CONTROL_BUFFER_SIZE;
  2122. buf = kmalloc(len, GFP_KERNEL);
  2123. if (!buf)
  2124. goto end;
  2125. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2126. kfree(buf);
  2127. }
  2128. end:
  2129. if (update_jiffies >= HZ)
  2130. update_jiffies = round_jiffies_relative(update_jiffies);
  2131. else {
  2132. j = round_jiffies_relative(update_jiffies);
  2133. if (abs(j - update_jiffies) <= 10)
  2134. update_jiffies = j;
  2135. }
  2136. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2137. update_jiffies);
  2138. }
  2139. /*
  2140. * driver/device initialization
  2141. */
  2142. static int bcm4320a_early_init(struct usbnet *usbdev)
  2143. {
  2144. /* bcm4320a doesn't handle configuration parameters well. Try
  2145. * set any and you get partially zeroed mac and broken device.
  2146. */
  2147. return 0;
  2148. }
  2149. static int bcm4320b_early_init(struct usbnet *usbdev)
  2150. {
  2151. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2152. char buf[8];
  2153. /* Early initialization settings, setting these won't have effect
  2154. * if called after generic_rndis_bind().
  2155. */
  2156. priv->param_country[0] = modparam_country[0];
  2157. priv->param_country[1] = modparam_country[1];
  2158. priv->param_country[2] = 0;
  2159. priv->param_frameburst = modparam_frameburst;
  2160. priv->param_afterburner = modparam_afterburner;
  2161. priv->param_power_save = modparam_power_save;
  2162. priv->param_power_output = modparam_power_output;
  2163. priv->param_roamtrigger = modparam_roamtrigger;
  2164. priv->param_roamdelta = modparam_roamdelta;
  2165. priv->param_country[0] = toupper(priv->param_country[0]);
  2166. priv->param_country[1] = toupper(priv->param_country[1]);
  2167. /* doesn't support EU as country code, use FI instead */
  2168. if (!strcmp(priv->param_country, "EU"))
  2169. strcpy(priv->param_country, "FI");
  2170. if (priv->param_power_save < 0)
  2171. priv->param_power_save = 0;
  2172. else if (priv->param_power_save > 2)
  2173. priv->param_power_save = 2;
  2174. if (priv->param_power_output < 0)
  2175. priv->param_power_output = 0;
  2176. else if (priv->param_power_output > 3)
  2177. priv->param_power_output = 3;
  2178. if (priv->param_roamtrigger < -80)
  2179. priv->param_roamtrigger = -80;
  2180. else if (priv->param_roamtrigger > -60)
  2181. priv->param_roamtrigger = -60;
  2182. if (priv->param_roamdelta < 0)
  2183. priv->param_roamdelta = 0;
  2184. else if (priv->param_roamdelta > 2)
  2185. priv->param_roamdelta = 2;
  2186. if (modparam_workaround_interval < 0)
  2187. priv->param_workaround_interval = 500;
  2188. else
  2189. priv->param_workaround_interval = modparam_workaround_interval;
  2190. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2191. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2192. priv->param_frameburst ? "1" : "0");
  2193. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2194. priv->param_afterburner ? "1" : "0");
  2195. sprintf(buf, "%d", priv->param_power_save);
  2196. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2197. sprintf(buf, "%d", priv->param_power_output);
  2198. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2199. sprintf(buf, "%d", priv->param_roamtrigger);
  2200. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2201. sprintf(buf, "%d", priv->param_roamdelta);
  2202. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2203. return 0;
  2204. }
  2205. /* same as rndis_netdev_ops but with local multicast handler */
  2206. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2207. .ndo_open = usbnet_open,
  2208. .ndo_stop = usbnet_stop,
  2209. .ndo_start_xmit = usbnet_start_xmit,
  2210. .ndo_tx_timeout = usbnet_tx_timeout,
  2211. .ndo_set_mac_address = eth_mac_addr,
  2212. .ndo_validate_addr = eth_validate_addr,
  2213. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2214. };
  2215. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2216. {
  2217. struct wiphy *wiphy;
  2218. struct rndis_wlan_private *priv;
  2219. int retval, len;
  2220. __le32 tmp;
  2221. /* allocate wiphy and rndis private data
  2222. * NOTE: We only support a single virtual interface, so wiphy
  2223. * and wireless_dev are somewhat synonymous for this device.
  2224. */
  2225. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2226. if (!wiphy)
  2227. return -ENOMEM;
  2228. priv = wiphy_priv(wiphy);
  2229. usbdev->net->ieee80211_ptr = &priv->wdev;
  2230. priv->wdev.wiphy = wiphy;
  2231. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2232. /* These have to be initialized before calling generic_rndis_bind().
  2233. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2234. */
  2235. usbdev->driver_priv = priv;
  2236. priv->usbdev = usbdev;
  2237. mutex_init(&priv->command_lock);
  2238. /* because rndis_command() sleeps we need to use workqueue */
  2239. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2240. INIT_WORK(&priv->work, rndis_wlan_worker);
  2241. INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
  2242. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2243. /* try bind rndis_host */
  2244. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2245. if (retval < 0)
  2246. goto fail;
  2247. /* generic_rndis_bind set packet filter to multicast_all+
  2248. * promisc mode which doesn't work well for our devices (device
  2249. * picks up rssi to closest station instead of to access point).
  2250. *
  2251. * rndis_host wants to avoid all OID as much as possible
  2252. * so do promisc/multicast handling in rndis_wlan.
  2253. */
  2254. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2255. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2256. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2257. sizeof(tmp));
  2258. len = sizeof(tmp);
  2259. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2260. &len);
  2261. priv->multicast_size = le32_to_cpu(tmp);
  2262. if (retval < 0 || priv->multicast_size < 0)
  2263. priv->multicast_size = 0;
  2264. if (priv->multicast_size > 0)
  2265. usbdev->net->flags |= IFF_MULTICAST;
  2266. else
  2267. usbdev->net->flags &= ~IFF_MULTICAST;
  2268. /* fill-out wiphy structure and register w/ cfg80211 */
  2269. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2270. wiphy->privid = rndis_wiphy_privid;
  2271. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2272. | BIT(NL80211_IFTYPE_ADHOC);
  2273. wiphy->max_scan_ssids = 1;
  2274. /* TODO: fill-out band/encr information based on priv->caps */
  2275. rndis_wlan_get_caps(usbdev);
  2276. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2277. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2278. priv->band.channels = priv->channels;
  2279. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2280. priv->band.bitrates = priv->rates;
  2281. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2282. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2283. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2284. memcpy(priv->cipher_suites, rndis_cipher_suites,
  2285. sizeof(rndis_cipher_suites));
  2286. wiphy->cipher_suites = priv->cipher_suites;
  2287. wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
  2288. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2289. if (wiphy_register(wiphy)) {
  2290. retval = -ENODEV;
  2291. goto fail;
  2292. }
  2293. set_default_iw_params(usbdev);
  2294. /* set default rts/frag */
  2295. rndis_set_wiphy_params(wiphy,
  2296. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2297. /* turn radio on */
  2298. priv->radio_on = true;
  2299. disassociate(usbdev, true);
  2300. netif_carrier_off(usbdev->net);
  2301. return 0;
  2302. fail:
  2303. cancel_delayed_work_sync(&priv->dev_poller_work);
  2304. cancel_delayed_work_sync(&priv->scan_work);
  2305. cancel_work_sync(&priv->work);
  2306. flush_workqueue(priv->workqueue);
  2307. destroy_workqueue(priv->workqueue);
  2308. wiphy_free(wiphy);
  2309. return retval;
  2310. }
  2311. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2312. {
  2313. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2314. /* turn radio off */
  2315. disassociate(usbdev, false);
  2316. cancel_delayed_work_sync(&priv->dev_poller_work);
  2317. cancel_delayed_work_sync(&priv->scan_work);
  2318. cancel_work_sync(&priv->work);
  2319. flush_workqueue(priv->workqueue);
  2320. destroy_workqueue(priv->workqueue);
  2321. if (priv && priv->wpa_ie_len)
  2322. kfree(priv->wpa_ie);
  2323. rndis_unbind(usbdev, intf);
  2324. wiphy_unregister(priv->wdev.wiphy);
  2325. wiphy_free(priv->wdev.wiphy);
  2326. }
  2327. static int rndis_wlan_reset(struct usbnet *usbdev)
  2328. {
  2329. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2330. int retval;
  2331. devdbg(usbdev, "rndis_wlan_reset");
  2332. retval = rndis_reset(usbdev);
  2333. if (retval)
  2334. devwarn(usbdev, "rndis_reset() failed: %d", retval);
  2335. /* rndis_reset cleared multicast list, so restore here.
  2336. (set_multicast_list() also turns on current packet filter) */
  2337. set_multicast_list(usbdev);
  2338. queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
  2339. round_jiffies_relative(DEVICE_POLLER_JIFFIES));
  2340. return deauthenticate(usbdev);
  2341. }
  2342. static int rndis_wlan_stop(struct usbnet *usbdev)
  2343. {
  2344. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2345. int retval;
  2346. __le32 filter;
  2347. devdbg(usbdev, "rndis_wlan_stop");
  2348. retval = disassociate(usbdev, false);
  2349. priv->work_pending = 0;
  2350. cancel_delayed_work_sync(&priv->dev_poller_work);
  2351. cancel_delayed_work_sync(&priv->scan_work);
  2352. cancel_work_sync(&priv->work);
  2353. flush_workqueue(priv->workqueue);
  2354. if (priv->scan_request) {
  2355. cfg80211_scan_done(priv->scan_request, true);
  2356. priv->scan_request = NULL;
  2357. }
  2358. /* Set current packet filter zero to block receiving data packets from
  2359. device. */
  2360. filter = 0;
  2361. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2362. sizeof(filter));
  2363. return retval;
  2364. }
  2365. static const struct driver_info bcm4320b_info = {
  2366. .description = "Wireless RNDIS device, BCM4320b based",
  2367. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2368. FLAG_AVOID_UNLINK_URBS,
  2369. .bind = rndis_wlan_bind,
  2370. .unbind = rndis_wlan_unbind,
  2371. .status = rndis_status,
  2372. .rx_fixup = rndis_rx_fixup,
  2373. .tx_fixup = rndis_tx_fixup,
  2374. .reset = rndis_wlan_reset,
  2375. .stop = rndis_wlan_stop,
  2376. .early_init = bcm4320b_early_init,
  2377. .indication = rndis_wlan_indication,
  2378. };
  2379. static const struct driver_info bcm4320a_info = {
  2380. .description = "Wireless RNDIS device, BCM4320a based",
  2381. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2382. FLAG_AVOID_UNLINK_URBS,
  2383. .bind = rndis_wlan_bind,
  2384. .unbind = rndis_wlan_unbind,
  2385. .status = rndis_status,
  2386. .rx_fixup = rndis_rx_fixup,
  2387. .tx_fixup = rndis_tx_fixup,
  2388. .reset = rndis_wlan_reset,
  2389. .stop = rndis_wlan_stop,
  2390. .early_init = bcm4320a_early_init,
  2391. .indication = rndis_wlan_indication,
  2392. };
  2393. static const struct driver_info rndis_wlan_info = {
  2394. .description = "Wireless RNDIS device",
  2395. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2396. FLAG_AVOID_UNLINK_URBS,
  2397. .bind = rndis_wlan_bind,
  2398. .unbind = rndis_wlan_unbind,
  2399. .status = rndis_status,
  2400. .rx_fixup = rndis_rx_fixup,
  2401. .tx_fixup = rndis_tx_fixup,
  2402. .reset = rndis_wlan_reset,
  2403. .stop = rndis_wlan_stop,
  2404. .early_init = bcm4320a_early_init,
  2405. .indication = rndis_wlan_indication,
  2406. };
  2407. /*-------------------------------------------------------------------------*/
  2408. static const struct usb_device_id products [] = {
  2409. #define RNDIS_MASTER_INTERFACE \
  2410. .bInterfaceClass = USB_CLASS_COMM, \
  2411. .bInterfaceSubClass = 2 /* ACM */, \
  2412. .bInterfaceProtocol = 0x0ff
  2413. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2414. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2415. */
  2416. {
  2417. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2418. | USB_DEVICE_ID_MATCH_DEVICE,
  2419. .idVendor = 0x0411,
  2420. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2421. RNDIS_MASTER_INTERFACE,
  2422. .driver_info = (unsigned long) &bcm4320b_info,
  2423. }, {
  2424. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2425. | USB_DEVICE_ID_MATCH_DEVICE,
  2426. .idVendor = 0x0baf,
  2427. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2428. RNDIS_MASTER_INTERFACE,
  2429. .driver_info = (unsigned long) &bcm4320b_info,
  2430. }, {
  2431. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2432. | USB_DEVICE_ID_MATCH_DEVICE,
  2433. .idVendor = 0x050d,
  2434. .idProduct = 0x011b, /* Belkin F5D7051 */
  2435. RNDIS_MASTER_INTERFACE,
  2436. .driver_info = (unsigned long) &bcm4320b_info,
  2437. }, {
  2438. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2439. | USB_DEVICE_ID_MATCH_DEVICE,
  2440. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2441. .idProduct = 0x011b, /* Belkin F5D7051 */
  2442. RNDIS_MASTER_INTERFACE,
  2443. .driver_info = (unsigned long) &bcm4320b_info,
  2444. }, {
  2445. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2446. | USB_DEVICE_ID_MATCH_DEVICE,
  2447. .idVendor = 0x13b1,
  2448. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2449. RNDIS_MASTER_INTERFACE,
  2450. .driver_info = (unsigned long) &bcm4320b_info,
  2451. }, {
  2452. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2453. | USB_DEVICE_ID_MATCH_DEVICE,
  2454. .idVendor = 0x13b1,
  2455. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2456. RNDIS_MASTER_INTERFACE,
  2457. .driver_info = (unsigned long) &bcm4320b_info,
  2458. }, {
  2459. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2460. | USB_DEVICE_ID_MATCH_DEVICE,
  2461. .idVendor = 0x0b05,
  2462. .idProduct = 0x1717, /* Asus WL169gE */
  2463. RNDIS_MASTER_INTERFACE,
  2464. .driver_info = (unsigned long) &bcm4320b_info,
  2465. }, {
  2466. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2467. | USB_DEVICE_ID_MATCH_DEVICE,
  2468. .idVendor = 0x0a5c,
  2469. .idProduct = 0xd11b, /* Eminent EM4045 */
  2470. RNDIS_MASTER_INTERFACE,
  2471. .driver_info = (unsigned long) &bcm4320b_info,
  2472. }, {
  2473. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2474. | USB_DEVICE_ID_MATCH_DEVICE,
  2475. .idVendor = 0x1690,
  2476. .idProduct = 0x0715, /* BT Voyager 1055 */
  2477. RNDIS_MASTER_INTERFACE,
  2478. .driver_info = (unsigned long) &bcm4320b_info,
  2479. },
  2480. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2481. * parameters available, hardware probably contain older firmware version with
  2482. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2483. */
  2484. {
  2485. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2486. | USB_DEVICE_ID_MATCH_DEVICE,
  2487. .idVendor = 0x13b1,
  2488. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2489. RNDIS_MASTER_INTERFACE,
  2490. .driver_info = (unsigned long) &bcm4320a_info,
  2491. }, {
  2492. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2493. | USB_DEVICE_ID_MATCH_DEVICE,
  2494. .idVendor = 0x0baf,
  2495. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2496. RNDIS_MASTER_INTERFACE,
  2497. .driver_info = (unsigned long) &bcm4320a_info,
  2498. }, {
  2499. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2500. | USB_DEVICE_ID_MATCH_DEVICE,
  2501. .idVendor = 0x0411,
  2502. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2503. RNDIS_MASTER_INTERFACE,
  2504. .driver_info = (unsigned long) &bcm4320a_info,
  2505. },
  2506. /* Generic Wireless RNDIS devices that we don't have exact
  2507. * idVendor/idProduct/chip yet.
  2508. */
  2509. {
  2510. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2511. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2512. .driver_info = (unsigned long) &rndis_wlan_info,
  2513. }, {
  2514. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2515. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2516. .driver_info = (unsigned long) &rndis_wlan_info,
  2517. },
  2518. { }, // END
  2519. };
  2520. MODULE_DEVICE_TABLE(usb, products);
  2521. static struct usb_driver rndis_wlan_driver = {
  2522. .name = "rndis_wlan",
  2523. .id_table = products,
  2524. .probe = usbnet_probe,
  2525. .disconnect = usbnet_disconnect,
  2526. .suspend = usbnet_suspend,
  2527. .resume = usbnet_resume,
  2528. };
  2529. static int __init rndis_wlan_init(void)
  2530. {
  2531. return usb_register(&rndis_wlan_driver);
  2532. }
  2533. module_init(rndis_wlan_init);
  2534. static void __exit rndis_wlan_exit(void)
  2535. {
  2536. usb_deregister(&rndis_wlan_driver);
  2537. }
  2538. module_exit(rndis_wlan_exit);
  2539. MODULE_AUTHOR("Bjorge Dijkstra");
  2540. MODULE_AUTHOR("Jussi Kivilinna");
  2541. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2542. MODULE_LICENSE("GPL");