rndis_wlan.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191
  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. enum ndis_80211_media_stream_mode media_stream_mode;
  219. enum ndis_80211_radio_status 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. /* these have to match what is in wpa_supplicant */
  312. enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
  313. enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
  314. CIPHER_WEP104 };
  315. enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
  316. KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
  317. /*
  318. * private data
  319. */
  320. #define NET_TYPE_11FB 0
  321. #define CAP_MODE_80211A 1
  322. #define CAP_MODE_80211B 2
  323. #define CAP_MODE_80211G 4
  324. #define CAP_MODE_MASK 7
  325. #define WORK_LINK_UP (1<<0)
  326. #define WORK_LINK_DOWN (1<<1)
  327. #define WORK_SET_MULTICAST_LIST (1<<2)
  328. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  329. static const struct ieee80211_channel rndis_channels[] = {
  330. { .center_freq = 2412 },
  331. { .center_freq = 2417 },
  332. { .center_freq = 2422 },
  333. { .center_freq = 2427 },
  334. { .center_freq = 2432 },
  335. { .center_freq = 2437 },
  336. { .center_freq = 2442 },
  337. { .center_freq = 2447 },
  338. { .center_freq = 2452 },
  339. { .center_freq = 2457 },
  340. { .center_freq = 2462 },
  341. { .center_freq = 2467 },
  342. { .center_freq = 2472 },
  343. { .center_freq = 2484 },
  344. };
  345. static const struct ieee80211_rate rndis_rates[] = {
  346. { .bitrate = 10 },
  347. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  348. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  349. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  350. { .bitrate = 60 },
  351. { .bitrate = 90 },
  352. { .bitrate = 120 },
  353. { .bitrate = 180 },
  354. { .bitrate = 240 },
  355. { .bitrate = 360 },
  356. { .bitrate = 480 },
  357. { .bitrate = 540 }
  358. };
  359. struct rndis_wlan_encr_key {
  360. int len;
  361. int cipher;
  362. u8 material[32];
  363. u8 bssid[ETH_ALEN];
  364. bool pairwise;
  365. bool tx_key;
  366. };
  367. /* RNDIS device private data */
  368. struct rndis_wlan_private {
  369. struct usbnet *usbdev;
  370. struct wireless_dev wdev;
  371. struct cfg80211_scan_request *scan_request;
  372. struct workqueue_struct *workqueue;
  373. struct delayed_work stats_work;
  374. struct delayed_work scan_work;
  375. struct work_struct work;
  376. struct mutex command_lock;
  377. spinlock_t stats_lock;
  378. unsigned long work_pending;
  379. struct ieee80211_supported_band band;
  380. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  381. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  382. struct iw_statistics iwstats;
  383. struct iw_statistics privstats;
  384. int caps;
  385. int multicast_size;
  386. /* module parameters */
  387. char param_country[4];
  388. int param_frameburst;
  389. int param_afterburner;
  390. int param_power_save;
  391. int param_power_output;
  392. int param_roamtrigger;
  393. int param_roamdelta;
  394. u32 param_workaround_interval;
  395. /* hardware state */
  396. int radio_on;
  397. int infra_mode;
  398. struct ndis_80211_ssid essid;
  399. /* encryption stuff */
  400. int encr_tx_key_index;
  401. struct rndis_wlan_encr_key encr_keys[4];
  402. int wpa_version;
  403. int wpa_keymgmt;
  404. int wpa_authalg;
  405. int wpa_ie_len;
  406. u8 *wpa_ie;
  407. int wpa_cipher_pair;
  408. int wpa_cipher_group;
  409. u8 command_buffer[COMMAND_BUFFER_SIZE];
  410. };
  411. /*
  412. * cfg80211 ops
  413. */
  414. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  415. struct net_device *dev,
  416. enum nl80211_iftype type, u32 *flags,
  417. struct vif_params *params);
  418. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  419. struct cfg80211_scan_request *request);
  420. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  421. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  422. int dbm);
  423. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
  424. static struct cfg80211_ops rndis_config_ops = {
  425. .change_virtual_intf = rndis_change_virtual_intf,
  426. .scan = rndis_scan,
  427. .set_wiphy_params = rndis_set_wiphy_params,
  428. .set_tx_power = rndis_set_tx_power,
  429. .get_tx_power = rndis_get_tx_power,
  430. };
  431. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  432. static const unsigned char zero_bssid[ETH_ALEN] = {0,};
  433. static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
  434. 0xff, 0xff, 0xff };
  435. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  436. {
  437. return (struct rndis_wlan_private *)dev->driver_priv;
  438. }
  439. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  440. {
  441. switch (priv->param_power_output) {
  442. default:
  443. case 3:
  444. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  445. case 2:
  446. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  447. case 1:
  448. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  449. case 0:
  450. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  451. }
  452. }
  453. static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
  454. {
  455. int cipher = priv->encr_keys[idx].cipher;
  456. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  457. cipher == WLAN_CIPHER_SUITE_TKIP);
  458. }
  459. #ifdef DEBUG
  460. static const char *oid_to_string(__le32 oid)
  461. {
  462. switch (oid) {
  463. #define OID_STR(oid) case oid: return(#oid)
  464. /* from rndis_host.h */
  465. OID_STR(OID_802_3_PERMANENT_ADDRESS);
  466. OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
  467. OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
  468. OID_STR(OID_GEN_PHYSICAL_MEDIUM);
  469. /* from rndis_wlan.c */
  470. OID_STR(OID_GEN_LINK_SPEED);
  471. OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
  472. OID_STR(OID_GEN_XMIT_OK);
  473. OID_STR(OID_GEN_RCV_OK);
  474. OID_STR(OID_GEN_XMIT_ERROR);
  475. OID_STR(OID_GEN_RCV_ERROR);
  476. OID_STR(OID_GEN_RCV_NO_BUFFER);
  477. OID_STR(OID_802_3_CURRENT_ADDRESS);
  478. OID_STR(OID_802_3_MULTICAST_LIST);
  479. OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
  480. OID_STR(OID_802_11_BSSID);
  481. OID_STR(OID_802_11_SSID);
  482. OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
  483. OID_STR(OID_802_11_ADD_WEP);
  484. OID_STR(OID_802_11_REMOVE_WEP);
  485. OID_STR(OID_802_11_DISASSOCIATE);
  486. OID_STR(OID_802_11_AUTHENTICATION_MODE);
  487. OID_STR(OID_802_11_PRIVACY_FILTER);
  488. OID_STR(OID_802_11_BSSID_LIST_SCAN);
  489. OID_STR(OID_802_11_ENCRYPTION_STATUS);
  490. OID_STR(OID_802_11_ADD_KEY);
  491. OID_STR(OID_802_11_REMOVE_KEY);
  492. OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
  493. OID_STR(OID_802_11_PMKID);
  494. OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
  495. OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
  496. OID_STR(OID_802_11_TX_POWER_LEVEL);
  497. OID_STR(OID_802_11_RSSI);
  498. OID_STR(OID_802_11_RSSI_TRIGGER);
  499. OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
  500. OID_STR(OID_802_11_RTS_THRESHOLD);
  501. OID_STR(OID_802_11_SUPPORTED_RATES);
  502. OID_STR(OID_802_11_CONFIGURATION);
  503. OID_STR(OID_802_11_BSSID_LIST);
  504. #undef OID_STR
  505. }
  506. return "?";
  507. }
  508. #else
  509. static const char *oid_to_string(__le32 oid)
  510. {
  511. return "?";
  512. }
  513. #endif
  514. /* translate error code */
  515. static int rndis_error_status(__le32 rndis_status)
  516. {
  517. int ret = -EINVAL;
  518. switch (rndis_status) {
  519. case RNDIS_STATUS_SUCCESS:
  520. ret = 0;
  521. break;
  522. case RNDIS_STATUS_FAILURE:
  523. case RNDIS_STATUS_INVALID_DATA:
  524. ret = -EINVAL;
  525. break;
  526. case RNDIS_STATUS_NOT_SUPPORTED:
  527. ret = -EOPNOTSUPP;
  528. break;
  529. case RNDIS_STATUS_ADAPTER_NOT_READY:
  530. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  531. ret = -EBUSY;
  532. break;
  533. }
  534. return ret;
  535. }
  536. static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
  537. {
  538. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  539. union {
  540. void *buf;
  541. struct rndis_msg_hdr *header;
  542. struct rndis_query *get;
  543. struct rndis_query_c *get_c;
  544. } u;
  545. int ret, buflen;
  546. buflen = *len + sizeof(*u.get);
  547. if (buflen < CONTROL_BUFFER_SIZE)
  548. buflen = CONTROL_BUFFER_SIZE;
  549. if (buflen > COMMAND_BUFFER_SIZE) {
  550. u.buf = kmalloc(buflen, GFP_KERNEL);
  551. if (!u.buf)
  552. return -ENOMEM;
  553. } else {
  554. u.buf = priv->command_buffer;
  555. }
  556. mutex_lock(&priv->command_lock);
  557. memset(u.get, 0, sizeof *u.get);
  558. u.get->msg_type = RNDIS_MSG_QUERY;
  559. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  560. u.get->oid = oid;
  561. ret = rndis_command(dev, u.header, buflen);
  562. if (ret < 0)
  563. devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
  564. "(%08x)", oid_to_string(oid), ret,
  565. le32_to_cpu(u.get_c->status));
  566. if (ret == 0) {
  567. ret = le32_to_cpu(u.get_c->len);
  568. *len = (*len > ret) ? ret : *len;
  569. memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
  570. ret = rndis_error_status(u.get_c->status);
  571. if (ret < 0)
  572. devdbg(dev, "rndis_query_oid(%s): device returned "
  573. "error, 0x%08x (%d)", oid_to_string(oid),
  574. le32_to_cpu(u.get_c->status), ret);
  575. }
  576. mutex_unlock(&priv->command_lock);
  577. if (u.buf != priv->command_buffer)
  578. kfree(u.buf);
  579. return ret;
  580. }
  581. static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
  582. {
  583. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  584. union {
  585. void *buf;
  586. struct rndis_msg_hdr *header;
  587. struct rndis_set *set;
  588. struct rndis_set_c *set_c;
  589. } u;
  590. int ret, buflen;
  591. buflen = len + sizeof(*u.set);
  592. if (buflen < CONTROL_BUFFER_SIZE)
  593. buflen = CONTROL_BUFFER_SIZE;
  594. if (buflen > COMMAND_BUFFER_SIZE) {
  595. u.buf = kmalloc(buflen, GFP_KERNEL);
  596. if (!u.buf)
  597. return -ENOMEM;
  598. } else {
  599. u.buf = priv->command_buffer;
  600. }
  601. mutex_lock(&priv->command_lock);
  602. memset(u.set, 0, sizeof *u.set);
  603. u.set->msg_type = RNDIS_MSG_SET;
  604. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  605. u.set->oid = oid;
  606. u.set->len = cpu_to_le32(len);
  607. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  608. u.set->handle = cpu_to_le32(0);
  609. memcpy(u.buf + sizeof(*u.set), data, len);
  610. ret = rndis_command(dev, u.header, buflen);
  611. if (ret < 0)
  612. devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
  613. "(%08x)", oid_to_string(oid), ret,
  614. le32_to_cpu(u.set_c->status));
  615. if (ret == 0) {
  616. ret = rndis_error_status(u.set_c->status);
  617. if (ret < 0)
  618. devdbg(dev, "rndis_set_oid(%s): device returned error, "
  619. "0x%08x (%d)", oid_to_string(oid),
  620. le32_to_cpu(u.set_c->status), ret);
  621. }
  622. mutex_unlock(&priv->command_lock);
  623. if (u.buf != priv->command_buffer)
  624. kfree(u.buf);
  625. return ret;
  626. }
  627. static int rndis_reset(struct usbnet *usbdev)
  628. {
  629. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  630. struct rndis_reset *reset;
  631. int ret;
  632. mutex_lock(&priv->command_lock);
  633. reset = (void *)priv->command_buffer;
  634. memset(reset, 0, sizeof(*reset));
  635. reset->msg_type = RNDIS_MSG_RESET;
  636. reset->msg_len = cpu_to_le32(sizeof(*reset));
  637. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  638. mutex_unlock(&priv->command_lock);
  639. if (ret < 0)
  640. return ret;
  641. return 0;
  642. }
  643. /*
  644. * Specs say that we can only set config parameters only soon after device
  645. * initialization.
  646. * value_type: 0 = u32, 2 = unicode string
  647. */
  648. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  649. int value_type, void *value)
  650. {
  651. struct ndis_config_param *infobuf;
  652. int value_len, info_len, param_len, ret, i;
  653. __le16 *unibuf;
  654. __le32 *dst_value;
  655. if (value_type == 0)
  656. value_len = sizeof(__le32);
  657. else if (value_type == 2)
  658. value_len = strlen(value) * sizeof(__le16);
  659. else
  660. return -EINVAL;
  661. param_len = strlen(param) * sizeof(__le16);
  662. info_len = sizeof(*infobuf) + param_len + value_len;
  663. #ifdef DEBUG
  664. info_len += 12;
  665. #endif
  666. infobuf = kmalloc(info_len, GFP_KERNEL);
  667. if (!infobuf)
  668. return -ENOMEM;
  669. #ifdef DEBUG
  670. info_len -= 12;
  671. /* extra 12 bytes are for padding (debug output) */
  672. memset(infobuf, 0xCC, info_len + 12);
  673. #endif
  674. if (value_type == 2)
  675. devdbg(dev, "setting config parameter: %s, value: %s",
  676. param, (u8 *)value);
  677. else
  678. devdbg(dev, "setting config parameter: %s, value: %d",
  679. param, *(u32 *)value);
  680. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  681. infobuf->name_length = cpu_to_le32(param_len);
  682. infobuf->type = cpu_to_le32(value_type);
  683. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  684. infobuf->value_length = cpu_to_le32(value_len);
  685. /* simple string to unicode string conversion */
  686. unibuf = (void *)infobuf + sizeof(*infobuf);
  687. for (i = 0; i < param_len / sizeof(__le16); i++)
  688. unibuf[i] = cpu_to_le16(param[i]);
  689. if (value_type == 2) {
  690. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  691. for (i = 0; i < value_len / sizeof(__le16); i++)
  692. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  693. } else {
  694. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  695. *dst_value = cpu_to_le32(*(u32 *)value);
  696. }
  697. #ifdef DEBUG
  698. devdbg(dev, "info buffer (len: %d):", info_len);
  699. for (i = 0; i < info_len; i += 12) {
  700. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  701. devdbg(dev, "%08X:%08X:%08X",
  702. cpu_to_be32(tmp[0]),
  703. cpu_to_be32(tmp[1]),
  704. cpu_to_be32(tmp[2]));
  705. }
  706. #endif
  707. ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
  708. infobuf, info_len);
  709. if (ret != 0)
  710. devdbg(dev, "setting rndis config parameter failed, %d.", ret);
  711. kfree(infobuf);
  712. return ret;
  713. }
  714. static int rndis_set_config_parameter_str(struct usbnet *dev,
  715. char *param, char *value)
  716. {
  717. return(rndis_set_config_parameter(dev, param, 2, value));
  718. }
  719. /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
  720. char *param, u32 value)
  721. {
  722. return(rndis_set_config_parameter(dev, param, 0, &value));
  723. }*/
  724. /*
  725. * data conversion functions
  726. */
  727. static int level_to_qual(int level)
  728. {
  729. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  730. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  731. }
  732. static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
  733. {
  734. freq->e = 0;
  735. freq->i = 0;
  736. freq->flags = 0;
  737. /* see comment in wireless.h above the "struct iw_freq"
  738. * definition for an explanation of this if
  739. * NOTE: 1000000 is due to the kHz
  740. */
  741. if (dsconfig > 1000000) {
  742. freq->m = dsconfig / 10;
  743. freq->e = 1;
  744. } else
  745. freq->m = dsconfig;
  746. /* convert from kHz to Hz */
  747. freq->e += 3;
  748. }
  749. static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
  750. {
  751. if (freq->m < 1000 && freq->e == 0) {
  752. if (freq->m >= 1 && freq->m <= 14)
  753. *dsconfig = ieee80211_dsss_chan_to_freq(freq->m) * 1000;
  754. else
  755. return -1;
  756. } else {
  757. int i;
  758. *dsconfig = freq->m;
  759. for (i = freq->e; i > 0; i--)
  760. *dsconfig *= 10;
  761. *dsconfig /= 1000;
  762. }
  763. return 0;
  764. }
  765. /*
  766. * common functions
  767. */
  768. static void restore_keys(struct usbnet *usbdev);
  769. static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  770. {
  771. int ret, len;
  772. len = sizeof(*ssid);
  773. ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
  774. if (ret != 0)
  775. ssid->length = 0;
  776. #ifdef DEBUG
  777. {
  778. unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
  779. memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
  780. tmp[le32_to_cpu(ssid->length)] = 0;
  781. devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
  782. }
  783. #endif
  784. return ret;
  785. }
  786. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  787. {
  788. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  789. int ret;
  790. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  791. if (ret == 0) {
  792. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  793. priv->radio_on = 1;
  794. devdbg(usbdev, "set_essid: radio_on = 1");
  795. }
  796. return ret;
  797. }
  798. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  799. {
  800. int ret, len;
  801. len = ETH_ALEN;
  802. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  803. if (ret != 0)
  804. memset(bssid, 0, ETH_ALEN);
  805. return ret;
  806. }
  807. static int get_association_info(struct usbnet *usbdev,
  808. struct ndis_80211_assoc_info *info, int len)
  809. {
  810. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  811. info, &len);
  812. }
  813. static int is_associated(struct usbnet *usbdev)
  814. {
  815. u8 bssid[ETH_ALEN];
  816. int ret;
  817. ret = get_bssid(usbdev, bssid);
  818. return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
  819. }
  820. static int disassociate(struct usbnet *usbdev, int reset_ssid)
  821. {
  822. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  823. struct ndis_80211_ssid ssid;
  824. int i, ret = 0;
  825. if (priv->radio_on) {
  826. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  827. if (ret == 0) {
  828. priv->radio_on = 0;
  829. devdbg(usbdev, "disassociate: radio_on = 0");
  830. if (reset_ssid)
  831. msleep(100);
  832. }
  833. }
  834. /* disassociate causes radio to be turned off; if reset_ssid
  835. * is given, set random ssid to enable radio */
  836. if (reset_ssid) {
  837. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  838. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  839. ssid.essid[0] = 0x1;
  840. ssid.essid[1] = 0xff;
  841. for (i = 2; i < sizeof(ssid.essid); i++)
  842. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  843. ret = set_essid(usbdev, &ssid);
  844. }
  845. return ret;
  846. }
  847. static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
  848. {
  849. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  850. __le32 tmp;
  851. int auth_mode, ret;
  852. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  853. "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
  854. if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
  855. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  856. auth_mode = NDIS_80211_AUTH_WPA2;
  857. else
  858. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  859. } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
  860. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  861. auth_mode = NDIS_80211_AUTH_WPA;
  862. else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
  863. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  864. else
  865. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  866. } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
  867. if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
  868. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  869. else
  870. auth_mode = NDIS_80211_AUTH_SHARED;
  871. } else
  872. auth_mode = NDIS_80211_AUTH_OPEN;
  873. tmp = cpu_to_le32(auth_mode);
  874. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  875. sizeof(tmp));
  876. if (ret != 0) {
  877. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  878. return ret;
  879. }
  880. priv->wpa_version = wpa_version;
  881. priv->wpa_authalg = authalg;
  882. return 0;
  883. }
  884. static int set_priv_filter(struct usbnet *usbdev)
  885. {
  886. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  887. __le32 tmp;
  888. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  889. if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
  890. priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
  891. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  892. else
  893. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  894. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  895. sizeof(tmp));
  896. }
  897. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  898. {
  899. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  900. __le32 tmp;
  901. int encr_mode, ret;
  902. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  903. pairwise,
  904. groupwise);
  905. if (pairwise & IW_AUTH_CIPHER_CCMP)
  906. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  907. else if (pairwise & IW_AUTH_CIPHER_TKIP)
  908. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  909. else if (pairwise &
  910. (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
  911. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  912. else if (groupwise & IW_AUTH_CIPHER_CCMP)
  913. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  914. else if (groupwise & IW_AUTH_CIPHER_TKIP)
  915. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  916. else
  917. encr_mode = NDIS_80211_ENCR_DISABLED;
  918. tmp = cpu_to_le32(encr_mode);
  919. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  920. sizeof(tmp));
  921. if (ret != 0) {
  922. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  923. return ret;
  924. }
  925. priv->wpa_cipher_pair = pairwise;
  926. priv->wpa_cipher_group = groupwise;
  927. return 0;
  928. }
  929. static int set_assoc_params(struct usbnet *usbdev)
  930. {
  931. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  932. set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
  933. set_priv_filter(usbdev);
  934. set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
  935. return 0;
  936. }
  937. static int set_infra_mode(struct usbnet *usbdev, int mode)
  938. {
  939. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  940. __le32 tmp;
  941. int ret;
  942. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  943. tmp = cpu_to_le32(mode);
  944. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  945. sizeof(tmp));
  946. if (ret != 0) {
  947. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  948. return ret;
  949. }
  950. /* NDIS drivers clear keys when infrastructure mode is
  951. * changed. But Linux tools assume otherwise. So set the
  952. * keys */
  953. restore_keys(usbdev);
  954. priv->infra_mode = mode;
  955. return 0;
  956. }
  957. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  958. {
  959. __le32 tmp;
  960. devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
  961. if (rts_threshold < 0 || rts_threshold > 2347)
  962. rts_threshold = 2347;
  963. tmp = cpu_to_le32(rts_threshold);
  964. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  965. sizeof(tmp));
  966. }
  967. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  968. {
  969. __le32 tmp;
  970. devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
  971. if (frag_threshold < 256 || frag_threshold > 2346)
  972. frag_threshold = 2346;
  973. tmp = cpu_to_le32(frag_threshold);
  974. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  975. sizeof(tmp));
  976. }
  977. static void set_default_iw_params(struct usbnet *usbdev)
  978. {
  979. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  980. priv->wpa_keymgmt = 0;
  981. priv->wpa_version = 0;
  982. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  983. set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  984. IW_AUTH_ALG_OPEN_SYSTEM);
  985. set_priv_filter(usbdev);
  986. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  987. }
  988. static int deauthenticate(struct usbnet *usbdev)
  989. {
  990. int ret;
  991. ret = disassociate(usbdev, 1);
  992. set_default_iw_params(usbdev);
  993. return ret;
  994. }
  995. /* index must be 0 - N, as per NDIS */
  996. static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
  997. {
  998. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  999. struct ndis_80211_wep_key ndis_key;
  1000. int cipher, ret;
  1001. if ((key_len != 5 || key_len != 13) || index < 0 || index > 3)
  1002. return -EINVAL;
  1003. if (key_len == 5)
  1004. cipher = WLAN_CIPHER_SUITE_WEP40;
  1005. else
  1006. cipher = WLAN_CIPHER_SUITE_WEP104;
  1007. memset(&ndis_key, 0, sizeof(ndis_key));
  1008. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1009. ndis_key.length = cpu_to_le32(key_len);
  1010. ndis_key.index = cpu_to_le32(index);
  1011. memcpy(&ndis_key.material, key, key_len);
  1012. if (index == priv->encr_tx_key_index) {
  1013. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1014. ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
  1015. IW_AUTH_CIPHER_NONE);
  1016. if (ret)
  1017. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  1018. ret);
  1019. }
  1020. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  1021. sizeof(ndis_key));
  1022. if (ret != 0) {
  1023. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  1024. index+1, ret);
  1025. return ret;
  1026. }
  1027. priv->encr_keys[index].len = key_len;
  1028. priv->encr_keys[index].cipher = cipher;
  1029. memcpy(&priv->encr_keys[index].material, key, key_len);
  1030. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1031. return 0;
  1032. }
  1033. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1034. int index, const u8 *addr, const u8 *rx_seq, int cipher,
  1035. int flags)
  1036. {
  1037. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1038. struct ndis_80211_key ndis_key;
  1039. bool is_addr_ok;
  1040. int ret;
  1041. if (index < 0 || index >= 4) {
  1042. devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
  1043. return -EINVAL;
  1044. }
  1045. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1046. devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
  1047. key_len);
  1048. return -EINVAL;
  1049. }
  1050. if ((flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) && !rx_seq) {
  1051. devdbg(usbdev, "add_wpa_key: recv seq flag without buffer");
  1052. return -EINVAL;
  1053. }
  1054. is_addr_ok = addr && memcmp(addr, zero_bssid, ETH_ALEN) != 0 &&
  1055. memcmp(addr, ffff_bssid, ETH_ALEN) != 0;
  1056. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1057. devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
  1058. addr);
  1059. return -EINVAL;
  1060. }
  1061. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  1062. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1063. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1064. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1065. memset(&ndis_key, 0, sizeof(ndis_key));
  1066. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1067. sizeof(ndis_key.material) + key_len);
  1068. ndis_key.length = cpu_to_le32(key_len);
  1069. ndis_key.index = cpu_to_le32(index) | flags;
  1070. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1071. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1072. * different order than NDIS spec, so swap the order here. */
  1073. memcpy(ndis_key.material, key, 16);
  1074. memcpy(ndis_key.material + 16, key + 24, 8);
  1075. memcpy(ndis_key.material + 24, key + 16, 8);
  1076. } else
  1077. memcpy(ndis_key.material, key, key_len);
  1078. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1079. memcpy(ndis_key.rsc, rx_seq, 6);
  1080. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1081. /* pairwise key */
  1082. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1083. } else {
  1084. /* group key */
  1085. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1086. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  1087. else
  1088. get_bssid(usbdev, ndis_key.bssid);
  1089. }
  1090. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  1091. le32_to_cpu(ndis_key.size));
  1092. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  1093. if (ret != 0)
  1094. return ret;
  1095. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1096. priv->encr_keys[index].len = key_len;
  1097. priv->encr_keys[index].cipher = cipher;
  1098. memcpy(&priv->encr_keys[index].material, key, key_len);
  1099. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1100. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1101. else
  1102. memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
  1103. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1104. priv->encr_tx_key_index = index;
  1105. return 0;
  1106. }
  1107. static int restore_key(struct usbnet *usbdev, int key_idx)
  1108. {
  1109. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1110. struct rndis_wlan_encr_key key;
  1111. int flags;
  1112. key = priv->encr_keys[key_idx];
  1113. devdbg(usbdev, "restore_key: %i:%s:%i", key_idx,
  1114. is_wpa_key(priv, key_idx) ? "wpa" : "wep",
  1115. key.len);
  1116. if (key.len == 0)
  1117. return 0;
  1118. if (is_wpa_key(priv, key_idx)) {
  1119. flags = 0;
  1120. /*if (priv->encr_tx_key_index == key_idx)
  1121. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;*/
  1122. if (memcmp(key.bssid, zero_bssid, ETH_ALEN) != 0 &&
  1123. memcmp(key.bssid, ffff_bssid, ETH_ALEN) != 0)
  1124. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1125. return add_wpa_key(usbdev, key.material, key.len, key_idx,
  1126. key.bssid, NULL, key.cipher, flags);
  1127. }
  1128. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1129. }
  1130. static void restore_keys(struct usbnet *usbdev)
  1131. {
  1132. int i;
  1133. for (i = 0; i < 4; i++)
  1134. restore_key(usbdev, i);
  1135. }
  1136. static void clear_key(struct rndis_wlan_private *priv, int idx)
  1137. {
  1138. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1139. }
  1140. /* remove_key is for both wep and wpa */
  1141. static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
  1142. {
  1143. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1144. struct ndis_80211_remove_key remove_key;
  1145. __le32 keyindex;
  1146. bool is_wpa;
  1147. int ret;
  1148. if (priv->encr_keys[index].len == 0)
  1149. return 0;
  1150. is_wpa = is_wpa_key(priv, index);
  1151. devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
  1152. priv->encr_keys[index].len);
  1153. clear_key(priv, index);
  1154. if (is_wpa) {
  1155. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1156. remove_key.index = cpu_to_le32(index);
  1157. if (bssid) {
  1158. /* pairwise key */
  1159. if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
  1160. remove_key.index |=
  1161. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1162. memcpy(remove_key.bssid, bssid,
  1163. sizeof(remove_key.bssid));
  1164. } else
  1165. memset(remove_key.bssid, 0xff,
  1166. sizeof(remove_key.bssid));
  1167. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  1168. sizeof(remove_key));
  1169. if (ret != 0)
  1170. return ret;
  1171. } else {
  1172. keyindex = cpu_to_le32(index);
  1173. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  1174. sizeof(keyindex));
  1175. if (ret != 0) {
  1176. devwarn(usbdev,
  1177. "removing encryption key %d failed (%08X)",
  1178. index, ret);
  1179. return ret;
  1180. }
  1181. }
  1182. /* if it is transmit key, disable encryption */
  1183. if (index == priv->encr_tx_key_index)
  1184. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  1185. return 0;
  1186. }
  1187. static void set_multicast_list(struct usbnet *usbdev)
  1188. {
  1189. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1190. struct dev_mc_list *mclist;
  1191. __le32 filter;
  1192. int ret, i, size;
  1193. char *buf;
  1194. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1195. if (usbdev->net->flags & IFF_PROMISC) {
  1196. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  1197. RNDIS_PACKET_TYPE_ALL_LOCAL;
  1198. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  1199. usbdev->net->mc_count > priv->multicast_size) {
  1200. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1201. } else if (usbdev->net->mc_count > 0) {
  1202. size = min(priv->multicast_size, usbdev->net->mc_count);
  1203. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  1204. if (!buf) {
  1205. devwarn(usbdev,
  1206. "couldn't alloc %d bytes of memory",
  1207. size * ETH_ALEN);
  1208. return;
  1209. }
  1210. mclist = usbdev->net->mc_list;
  1211. for (i = 0; i < size && mclist; mclist = mclist->next) {
  1212. if (mclist->dmi_addrlen != ETH_ALEN)
  1213. continue;
  1214. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  1215. i++;
  1216. }
  1217. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  1218. i * ETH_ALEN);
  1219. if (ret == 0 && i > 0)
  1220. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  1221. else
  1222. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  1223. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  1224. i, priv->multicast_size, ret);
  1225. kfree(buf);
  1226. }
  1227. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1228. sizeof(filter));
  1229. if (ret < 0) {
  1230. devwarn(usbdev, "couldn't set packet filter: %08x",
  1231. le32_to_cpu(filter));
  1232. }
  1233. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1234. le32_to_cpu(filter), ret);
  1235. }
  1236. /*
  1237. * cfg80211 ops
  1238. */
  1239. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1240. struct net_device *dev,
  1241. enum nl80211_iftype type, u32 *flags,
  1242. struct vif_params *params)
  1243. {
  1244. struct usbnet *usbdev = netdev_priv(dev);
  1245. int mode;
  1246. switch (type) {
  1247. case NL80211_IFTYPE_ADHOC:
  1248. mode = NDIS_80211_INFRA_ADHOC;
  1249. break;
  1250. case NL80211_IFTYPE_STATION:
  1251. mode = NDIS_80211_INFRA_INFRA;
  1252. break;
  1253. default:
  1254. return -EINVAL;
  1255. }
  1256. return set_infra_mode(usbdev, mode);
  1257. }
  1258. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1259. {
  1260. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1261. struct usbnet *usbdev = priv->usbdev;
  1262. int err;
  1263. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1264. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1265. if (err < 0)
  1266. return err;
  1267. }
  1268. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1269. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1270. if (err < 0)
  1271. return err;
  1272. }
  1273. return 0;
  1274. }
  1275. static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
  1276. int dbm)
  1277. {
  1278. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1279. struct usbnet *usbdev = priv->usbdev;
  1280. devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
  1281. /* Device doesn't support changing txpower after initialization, only
  1282. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1283. * currently used.
  1284. */
  1285. if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
  1286. if (!priv->radio_on)
  1287. disassociate(usbdev, 1); /* turn on radio */
  1288. return 0;
  1289. }
  1290. return -ENOTSUPP;
  1291. }
  1292. static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
  1293. {
  1294. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1295. struct usbnet *usbdev = priv->usbdev;
  1296. *dbm = get_bcm4320_power_dbm(priv);
  1297. devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
  1298. return 0;
  1299. }
  1300. #define SCAN_DELAY_JIFFIES (HZ)
  1301. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1302. struct cfg80211_scan_request *request)
  1303. {
  1304. struct usbnet *usbdev = netdev_priv(dev);
  1305. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1306. int ret;
  1307. __le32 tmp;
  1308. devdbg(usbdev, "cfg80211.scan");
  1309. if (!request)
  1310. return -EINVAL;
  1311. if (priv->scan_request && priv->scan_request != request)
  1312. return -EBUSY;
  1313. priv->scan_request = request;
  1314. tmp = cpu_to_le32(1);
  1315. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1316. sizeof(tmp));
  1317. if (ret == 0) {
  1318. /* Wait before retrieving scan results from device */
  1319. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1320. SCAN_DELAY_JIFFIES);
  1321. }
  1322. return ret;
  1323. }
  1324. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1325. struct ndis_80211_bssid_ex *bssid)
  1326. {
  1327. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1328. struct ieee80211_channel *channel;
  1329. s32 signal;
  1330. u64 timestamp;
  1331. u16 capability;
  1332. u16 beacon_interval;
  1333. struct ndis_80211_fixed_ies *fixed;
  1334. int ie_len, bssid_len;
  1335. u8 *ie;
  1336. /* parse bssid structure */
  1337. bssid_len = le32_to_cpu(bssid->length);
  1338. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1339. sizeof(struct ndis_80211_fixed_ies))
  1340. return NULL;
  1341. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1342. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1343. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1344. (int)le32_to_cpu(bssid->ie_length));
  1345. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1346. if (ie_len < 0)
  1347. return NULL;
  1348. /* extract data for cfg80211_inform_bss */
  1349. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1350. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1351. if (!channel)
  1352. return NULL;
  1353. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1354. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1355. capability = le16_to_cpu(fixed->capabilities);
  1356. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1357. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1358. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1359. GFP_KERNEL);
  1360. }
  1361. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1362. {
  1363. void *buf = NULL;
  1364. struct ndis_80211_bssid_list_ex *bssid_list;
  1365. struct ndis_80211_bssid_ex *bssid;
  1366. int ret = -EINVAL, len, count, bssid_len;
  1367. devdbg(usbdev, "check_bssid_list");
  1368. len = CONTROL_BUFFER_SIZE;
  1369. buf = kmalloc(len, GFP_KERNEL);
  1370. if (!buf) {
  1371. ret = -ENOMEM;
  1372. goto out;
  1373. }
  1374. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1375. if (ret != 0)
  1376. goto out;
  1377. bssid_list = buf;
  1378. bssid = bssid_list->bssid;
  1379. bssid_len = le32_to_cpu(bssid->length);
  1380. count = le32_to_cpu(bssid_list->num_items);
  1381. devdbg(usbdev, "check_bssid_list: %d BSSIDs found", count);
  1382. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1383. rndis_bss_info_update(usbdev, bssid);
  1384. bssid = (void *)bssid + bssid_len;
  1385. bssid_len = le32_to_cpu(bssid->length);
  1386. count--;
  1387. }
  1388. out:
  1389. kfree(buf);
  1390. return ret;
  1391. }
  1392. static void rndis_get_scan_results(struct work_struct *work)
  1393. {
  1394. struct rndis_wlan_private *priv =
  1395. container_of(work, struct rndis_wlan_private, scan_work.work);
  1396. struct usbnet *usbdev = priv->usbdev;
  1397. int ret;
  1398. devdbg(usbdev, "get_scan_results");
  1399. if (!priv->scan_request)
  1400. return;
  1401. ret = rndis_check_bssid_list(usbdev);
  1402. cfg80211_scan_done(priv->scan_request, ret < 0);
  1403. priv->scan_request = NULL;
  1404. }
  1405. /*
  1406. * wireless extension handlers
  1407. */
  1408. static int rndis_iw_commit(struct net_device *dev,
  1409. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1410. {
  1411. /* dummy op */
  1412. return 0;
  1413. }
  1414. static int rndis_iw_set_essid(struct net_device *dev,
  1415. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1416. {
  1417. struct ndis_80211_ssid ssid;
  1418. int length = wrqu->essid.length;
  1419. struct usbnet *usbdev = netdev_priv(dev);
  1420. devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
  1421. wrqu->essid.flags, wrqu->essid.length, essid);
  1422. if (length > NDIS_802_11_LENGTH_SSID)
  1423. length = NDIS_802_11_LENGTH_SSID;
  1424. ssid.length = cpu_to_le32(length);
  1425. if (length > 0)
  1426. memcpy(ssid.essid, essid, length);
  1427. else
  1428. memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
  1429. set_assoc_params(usbdev);
  1430. if (!wrqu->essid.flags || length == 0)
  1431. return disassociate(usbdev, 1);
  1432. else
  1433. return set_essid(usbdev, &ssid);
  1434. }
  1435. static int rndis_iw_get_essid(struct net_device *dev,
  1436. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1437. {
  1438. struct ndis_80211_ssid ssid;
  1439. struct usbnet *usbdev = netdev_priv(dev);
  1440. int ret;
  1441. ret = get_essid(usbdev, &ssid);
  1442. if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
  1443. wrqu->essid.flags = 1;
  1444. wrqu->essid.length = le32_to_cpu(ssid.length);
  1445. memcpy(essid, ssid.essid, wrqu->essid.length);
  1446. essid[wrqu->essid.length] = 0;
  1447. } else {
  1448. memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
  1449. wrqu->essid.flags = 0;
  1450. wrqu->essid.length = 0;
  1451. }
  1452. devdbg(usbdev, "SIOCGIWESSID: %s", essid);
  1453. return ret;
  1454. }
  1455. static int rndis_iw_get_bssid(struct net_device *dev,
  1456. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1457. {
  1458. struct usbnet *usbdev = netdev_priv(dev);
  1459. unsigned char bssid[ETH_ALEN];
  1460. int ret;
  1461. ret = get_bssid(usbdev, bssid);
  1462. if (ret == 0)
  1463. devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
  1464. else
  1465. devdbg(usbdev, "SIOCGIWAP: <not associated>");
  1466. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1467. memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
  1468. return ret;
  1469. }
  1470. static int rndis_iw_set_bssid(struct net_device *dev,
  1471. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1472. {
  1473. struct usbnet *usbdev = netdev_priv(dev);
  1474. u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
  1475. int ret;
  1476. devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
  1477. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  1478. /* user apps may set ap's mac address, which is not required;
  1479. * they may fail to work if this function fails, so return
  1480. * success */
  1481. if (ret)
  1482. devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
  1483. return 0;
  1484. }
  1485. static int rndis_iw_set_auth(struct net_device *dev,
  1486. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1487. {
  1488. struct iw_param *p = &wrqu->param;
  1489. struct usbnet *usbdev = netdev_priv(dev);
  1490. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1491. int ret = -ENOTSUPP;
  1492. switch (p->flags & IW_AUTH_INDEX) {
  1493. case IW_AUTH_WPA_VERSION:
  1494. devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
  1495. priv->wpa_version = p->value;
  1496. ret = 0;
  1497. break;
  1498. case IW_AUTH_CIPHER_PAIRWISE:
  1499. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
  1500. priv->wpa_cipher_pair = p->value;
  1501. ret = 0;
  1502. break;
  1503. case IW_AUTH_CIPHER_GROUP:
  1504. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
  1505. priv->wpa_cipher_group = p->value;
  1506. ret = 0;
  1507. break;
  1508. case IW_AUTH_KEY_MGMT:
  1509. devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
  1510. priv->wpa_keymgmt = p->value;
  1511. ret = 0;
  1512. break;
  1513. case IW_AUTH_TKIP_COUNTERMEASURES:
  1514. devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
  1515. p->value);
  1516. ret = 0;
  1517. break;
  1518. case IW_AUTH_DROP_UNENCRYPTED:
  1519. devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
  1520. ret = 0;
  1521. break;
  1522. case IW_AUTH_80211_AUTH_ALG:
  1523. devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
  1524. priv->wpa_authalg = p->value;
  1525. ret = 0;
  1526. break;
  1527. case IW_AUTH_WPA_ENABLED:
  1528. devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
  1529. if (wrqu->param.value)
  1530. deauthenticate(usbdev);
  1531. ret = 0;
  1532. break;
  1533. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  1534. devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
  1535. p->value);
  1536. ret = 0;
  1537. break;
  1538. case IW_AUTH_ROAMING_CONTROL:
  1539. devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
  1540. ret = 0;
  1541. break;
  1542. case IW_AUTH_PRIVACY_INVOKED:
  1543. devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
  1544. wrqu->param.flags & IW_AUTH_INDEX);
  1545. return -EOPNOTSUPP;
  1546. default:
  1547. devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
  1548. p->flags & IW_AUTH_INDEX, p->value);
  1549. }
  1550. return ret;
  1551. }
  1552. static int rndis_iw_get_auth(struct net_device *dev,
  1553. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1554. {
  1555. struct iw_param *p = &wrqu->param;
  1556. struct usbnet *usbdev = netdev_priv(dev);
  1557. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1558. switch (p->flags & IW_AUTH_INDEX) {
  1559. case IW_AUTH_WPA_VERSION:
  1560. p->value = priv->wpa_version;
  1561. break;
  1562. case IW_AUTH_CIPHER_PAIRWISE:
  1563. p->value = priv->wpa_cipher_pair;
  1564. break;
  1565. case IW_AUTH_CIPHER_GROUP:
  1566. p->value = priv->wpa_cipher_group;
  1567. break;
  1568. case IW_AUTH_KEY_MGMT:
  1569. p->value = priv->wpa_keymgmt;
  1570. break;
  1571. case IW_AUTH_80211_AUTH_ALG:
  1572. p->value = priv->wpa_authalg;
  1573. break;
  1574. default:
  1575. devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
  1576. wrqu->param.flags & IW_AUTH_INDEX);
  1577. return -EOPNOTSUPP;
  1578. }
  1579. return 0;
  1580. }
  1581. static int rndis_iw_set_encode(struct net_device *dev,
  1582. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1583. {
  1584. struct usbnet *usbdev = netdev_priv(dev);
  1585. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1586. struct rndis_wlan_encr_key key;
  1587. int ret, index, key_len;
  1588. u8 *keybuf;
  1589. index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
  1590. /* iwconfig gives index as 1 - N */
  1591. if (index > 0)
  1592. index--;
  1593. else
  1594. index = priv->encr_tx_key_index;
  1595. if (index < 0 || index >= 4) {
  1596. devwarn(usbdev, "encryption index out of range (%u)", index);
  1597. return -EINVAL;
  1598. }
  1599. /* remove key if disabled */
  1600. if (wrqu->data.flags & IW_ENCODE_DISABLED) {
  1601. if (remove_key(usbdev, index, NULL))
  1602. return -EINVAL;
  1603. else
  1604. return 0;
  1605. }
  1606. /* global encryption state (for all keys) */
  1607. if (wrqu->data.flags & IW_ENCODE_OPEN)
  1608. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1609. IW_AUTH_ALG_OPEN_SYSTEM);
  1610. else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
  1611. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1612. IW_AUTH_ALG_SHARED_KEY);
  1613. if (ret != 0)
  1614. return ret;
  1615. if (wrqu->data.length > 0) {
  1616. key_len = wrqu->data.length;
  1617. keybuf = extra;
  1618. } else {
  1619. /* must be set as tx key */
  1620. if (priv->encr_keys[index].len == 0)
  1621. return -EINVAL;
  1622. key = priv->encr_keys[index];
  1623. key_len = key.len;
  1624. keybuf = key.material;
  1625. priv->encr_tx_key_index = index;
  1626. }
  1627. if (add_wep_key(usbdev, keybuf, key_len, index) != 0)
  1628. return -EINVAL;
  1629. if (index == priv->encr_tx_key_index)
  1630. /* ndis drivers want essid to be set after setting encr */
  1631. set_essid(usbdev, &priv->essid);
  1632. return 0;
  1633. }
  1634. static int rndis_iw_set_encode_ext(struct net_device *dev,
  1635. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1636. {
  1637. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1638. struct usbnet *usbdev = netdev_priv(dev);
  1639. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1640. int keyidx, flags, cipher;
  1641. keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
  1642. /* iwconfig gives index as 1 - N */
  1643. if (keyidx)
  1644. keyidx--;
  1645. else
  1646. keyidx = priv->encr_tx_key_index;
  1647. if (keyidx < 0 || keyidx >= 4) {
  1648. devwarn(usbdev, "encryption index out of range (%u)", keyidx);
  1649. return -EINVAL;
  1650. }
  1651. if (ext->alg == WPA_ALG_WEP) {
  1652. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1653. priv->encr_tx_key_index = keyidx;
  1654. return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
  1655. }
  1656. cipher = -1;
  1657. if (ext->alg == IW_ENCODE_ALG_TKIP)
  1658. cipher = WLAN_CIPHER_SUITE_TKIP;
  1659. else if (ext->alg == IW_ENCODE_ALG_CCMP)
  1660. cipher = WLAN_CIPHER_SUITE_CCMP;
  1661. if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
  1662. ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
  1663. return remove_key(usbdev, keyidx, NULL);
  1664. if (cipher == -1)
  1665. return -EOPNOTSUPP;
  1666. flags = 0;
  1667. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
  1668. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1669. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
  1670. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1671. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1672. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1673. return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx,
  1674. (u8 *)&ext->addr.sa_data, ext->rx_seq, cipher,
  1675. flags);
  1676. }
  1677. static int rndis_iw_set_genie(struct net_device *dev,
  1678. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1679. {
  1680. struct usbnet *usbdev = netdev_priv(dev);
  1681. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1682. int ret = 0;
  1683. #ifdef DEBUG
  1684. int j;
  1685. u8 *gie = extra;
  1686. for (j = 0; j < wrqu->data.length; j += 8)
  1687. devdbg(usbdev,
  1688. "SIOCSIWGENIE %04x - "
  1689. "%02x %02x %02x %02x %02x %02x %02x %02x", j,
  1690. gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
  1691. gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
  1692. #endif
  1693. /* clear existing IEs */
  1694. if (priv->wpa_ie_len) {
  1695. kfree(priv->wpa_ie);
  1696. priv->wpa_ie_len = 0;
  1697. }
  1698. /* set new IEs */
  1699. priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
  1700. if (priv->wpa_ie) {
  1701. priv->wpa_ie_len = wrqu->data.length;
  1702. memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
  1703. } else
  1704. ret = -ENOMEM;
  1705. return ret;
  1706. }
  1707. static int rndis_iw_get_genie(struct net_device *dev,
  1708. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1709. {
  1710. struct usbnet *usbdev = netdev_priv(dev);
  1711. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1712. devdbg(usbdev, "SIOCGIWGENIE");
  1713. if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
  1714. wrqu->data.length = 0;
  1715. return 0;
  1716. }
  1717. if (wrqu->data.length < priv->wpa_ie_len)
  1718. return -E2BIG;
  1719. wrqu->data.length = priv->wpa_ie_len;
  1720. memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
  1721. return 0;
  1722. }
  1723. static int rndis_iw_set_freq(struct net_device *dev,
  1724. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1725. {
  1726. struct usbnet *usbdev = netdev_priv(dev);
  1727. struct ndis_80211_conf config;
  1728. unsigned int dsconfig;
  1729. int len, ret;
  1730. /* this OID is valid only when not associated */
  1731. if (is_associated(usbdev))
  1732. return 0;
  1733. dsconfig = 0;
  1734. if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
  1735. return -EINVAL;
  1736. len = sizeof(config);
  1737. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1738. if (ret != 0) {
  1739. devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
  1740. return 0;
  1741. }
  1742. config.ds_config = cpu_to_le32(dsconfig);
  1743. devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
  1744. return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1745. sizeof(config));
  1746. }
  1747. static int rndis_iw_get_freq(struct net_device *dev,
  1748. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1749. {
  1750. struct usbnet *usbdev = netdev_priv(dev);
  1751. struct ndis_80211_conf config;
  1752. int len, ret;
  1753. len = sizeof(config);
  1754. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1755. if (ret == 0)
  1756. dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
  1757. devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
  1758. return ret;
  1759. }
  1760. static int rndis_iw_get_rate(struct net_device *dev,
  1761. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1762. {
  1763. struct usbnet *usbdev = netdev_priv(dev);
  1764. __le32 tmp;
  1765. int ret, len;
  1766. len = sizeof(tmp);
  1767. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
  1768. if (ret == 0) {
  1769. wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
  1770. wrqu->bitrate.disabled = 0;
  1771. wrqu->bitrate.flags = 1;
  1772. }
  1773. return ret;
  1774. }
  1775. static int rndis_iw_set_mlme(struct net_device *dev,
  1776. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1777. {
  1778. struct usbnet *usbdev = netdev_priv(dev);
  1779. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1780. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  1781. unsigned char bssid[ETH_ALEN];
  1782. get_bssid(usbdev, bssid);
  1783. if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
  1784. return -EINVAL;
  1785. switch (mlme->cmd) {
  1786. case IW_MLME_DEAUTH:
  1787. return deauthenticate(usbdev);
  1788. case IW_MLME_DISASSOC:
  1789. return disassociate(usbdev, priv->radio_on);
  1790. default:
  1791. return -EOPNOTSUPP;
  1792. }
  1793. return 0;
  1794. }
  1795. static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
  1796. {
  1797. struct usbnet *usbdev = netdev_priv(dev);
  1798. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1799. unsigned long flags;
  1800. spin_lock_irqsave(&priv->stats_lock, flags);
  1801. memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
  1802. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1803. return &priv->iwstats;
  1804. }
  1805. #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
  1806. static const iw_handler rndis_iw_handler[] =
  1807. {
  1808. IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
  1809. IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
  1810. IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
  1811. IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
  1812. IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
  1813. IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
  1814. IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
  1815. IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
  1816. IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
  1817. IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
  1818. IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
  1819. IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
  1820. IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
  1821. IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
  1822. IW_IOCTL(SIOCSIWRTS) = (iw_handler) cfg80211_wext_siwrts,
  1823. IW_IOCTL(SIOCGIWRTS) = (iw_handler) cfg80211_wext_giwrts,
  1824. IW_IOCTL(SIOCSIWFRAG) = (iw_handler) cfg80211_wext_siwfrag,
  1825. IW_IOCTL(SIOCGIWFRAG) = (iw_handler) cfg80211_wext_giwfrag,
  1826. IW_IOCTL(SIOCSIWTXPOW) = (iw_handler) cfg80211_wext_siwtxpower,
  1827. IW_IOCTL(SIOCGIWTXPOW) = (iw_handler) cfg80211_wext_giwtxpower,
  1828. IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
  1829. IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
  1830. IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
  1831. IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
  1832. IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
  1833. IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
  1834. IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
  1835. };
  1836. static const iw_handler rndis_wlan_private_handler[] = {
  1837. };
  1838. static const struct iw_priv_args rndis_wlan_private_args[] = {
  1839. };
  1840. static const struct iw_handler_def rndis_iw_handlers = {
  1841. .num_standard = ARRAY_SIZE(rndis_iw_handler),
  1842. .num_private = ARRAY_SIZE(rndis_wlan_private_handler),
  1843. .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
  1844. .standard = (iw_handler *)rndis_iw_handler,
  1845. .private = (iw_handler *)rndis_wlan_private_handler,
  1846. .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
  1847. .get_wireless_stats = rndis_get_wireless_stats,
  1848. };
  1849. static void rndis_wlan_worker(struct work_struct *work)
  1850. {
  1851. struct rndis_wlan_private *priv =
  1852. container_of(work, struct rndis_wlan_private, work);
  1853. struct usbnet *usbdev = priv->usbdev;
  1854. union iwreq_data evt;
  1855. unsigned char bssid[ETH_ALEN];
  1856. struct ndis_80211_assoc_info *info;
  1857. int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
  1858. int ret, offset;
  1859. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
  1860. netif_carrier_on(usbdev->net);
  1861. info = kzalloc(assoc_size, GFP_KERNEL);
  1862. if (!info)
  1863. goto get_bssid;
  1864. /* Get association info IEs from device and send them back to
  1865. * userspace. */
  1866. ret = get_association_info(usbdev, info, assoc_size);
  1867. if (!ret) {
  1868. evt.data.length = le32_to_cpu(info->req_ie_length);
  1869. if (evt.data.length > 0) {
  1870. offset = le32_to_cpu(info->offset_req_ies);
  1871. wireless_send_event(usbdev->net,
  1872. IWEVASSOCREQIE, &evt,
  1873. (char *)info + offset);
  1874. }
  1875. evt.data.length = le32_to_cpu(info->resp_ie_length);
  1876. if (evt.data.length > 0) {
  1877. offset = le32_to_cpu(info->offset_resp_ies);
  1878. wireless_send_event(usbdev->net,
  1879. IWEVASSOCRESPIE, &evt,
  1880. (char *)info + offset);
  1881. }
  1882. }
  1883. kfree(info);
  1884. get_bssid:
  1885. ret = get_bssid(usbdev, bssid);
  1886. if (!ret) {
  1887. evt.data.flags = 0;
  1888. evt.data.length = 0;
  1889. memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
  1890. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1891. }
  1892. }
  1893. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
  1894. netif_carrier_off(usbdev->net);
  1895. evt.data.flags = 0;
  1896. evt.data.length = 0;
  1897. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1898. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1899. }
  1900. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1901. set_multicast_list(usbdev);
  1902. }
  1903. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1904. {
  1905. struct usbnet *usbdev = netdev_priv(dev);
  1906. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1907. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1908. return;
  1909. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1910. queue_work(priv->workqueue, &priv->work);
  1911. }
  1912. static void rndis_wlan_auth_indication(struct usbnet *usbdev,
  1913. struct ndis_80211_status_indication *indication,
  1914. int len)
  1915. {
  1916. u8 *buf;
  1917. const char *type;
  1918. int flags, buflen;
  1919. bool pairwise_error, group_error;
  1920. struct ndis_80211_auth_request *auth_req;
  1921. /* must have at least one array entry */
  1922. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1923. sizeof(struct ndis_80211_auth_request)) {
  1924. devinfo(usbdev, "authentication indication: "
  1925. "too short message (%i)", len);
  1926. return;
  1927. }
  1928. buf = (void *)&indication->u.auth_request[0];
  1929. buflen = len - offsetof(struct ndis_80211_status_indication, u);
  1930. while (buflen >= sizeof(*auth_req)) {
  1931. auth_req = (void *)buf;
  1932. type = "unknown";
  1933. flags = le32_to_cpu(auth_req->flags);
  1934. pairwise_error = false;
  1935. group_error = false;
  1936. if (flags & 0x1)
  1937. type = "reauth request";
  1938. if (flags & 0x2)
  1939. type = "key update request";
  1940. if (flags & 0x6) {
  1941. pairwise_error = true;
  1942. type = "pairwise_error";
  1943. }
  1944. if (flags & 0xe) {
  1945. group_error = true;
  1946. type = "group_error";
  1947. }
  1948. devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
  1949. le32_to_cpu(auth_req->flags));
  1950. if (pairwise_error || group_error) {
  1951. union iwreq_data wrqu;
  1952. struct iw_michaelmicfailure micfailure;
  1953. memset(&micfailure, 0, sizeof(micfailure));
  1954. if (pairwise_error)
  1955. micfailure.flags |= IW_MICFAILURE_PAIRWISE;
  1956. if (group_error)
  1957. micfailure.flags |= IW_MICFAILURE_GROUP;
  1958. memcpy(micfailure.src_addr.sa_data, auth_req->bssid,
  1959. ETH_ALEN);
  1960. memset(&wrqu, 0, sizeof(wrqu));
  1961. wrqu.data.length = sizeof(micfailure);
  1962. wireless_send_event(usbdev->net, IWEVMICHAELMICFAILURE,
  1963. &wrqu, (u8 *)&micfailure);
  1964. }
  1965. buflen -= le32_to_cpu(auth_req->length);
  1966. buf += le32_to_cpu(auth_req->length);
  1967. }
  1968. }
  1969. static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
  1970. struct ndis_80211_status_indication *indication,
  1971. int len)
  1972. {
  1973. struct ndis_80211_pmkid_cand_list *cand_list;
  1974. int list_len, expected_len, i;
  1975. if (len < offsetof(struct ndis_80211_status_indication, u) +
  1976. sizeof(struct ndis_80211_pmkid_cand_list)) {
  1977. devinfo(usbdev, "pmkid candidate list indication: "
  1978. "too short message (%i)", len);
  1979. return;
  1980. }
  1981. list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
  1982. sizeof(struct ndis_80211_pmkid_candidate);
  1983. expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
  1984. offsetof(struct ndis_80211_status_indication, u);
  1985. if (len < expected_len) {
  1986. devinfo(usbdev, "pmkid candidate list indication: "
  1987. "list larger than buffer (%i < %i)",
  1988. len, expected_len);
  1989. return;
  1990. }
  1991. cand_list = &indication->u.cand_list;
  1992. devinfo(usbdev, "pmkid candidate list indication: "
  1993. "version %i, candidates %i",
  1994. le32_to_cpu(cand_list->version),
  1995. le32_to_cpu(cand_list->num_candidates));
  1996. if (le32_to_cpu(cand_list->version) != 1)
  1997. return;
  1998. for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
  1999. struct iw_pmkid_cand pcand;
  2000. union iwreq_data wrqu;
  2001. struct ndis_80211_pmkid_candidate *cand =
  2002. &cand_list->candidate_list[i];
  2003. devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
  2004. i, le32_to_cpu(cand->flags), cand->bssid);
  2005. memset(&pcand, 0, sizeof(pcand));
  2006. if (le32_to_cpu(cand->flags) & 0x01)
  2007. pcand.flags |= IW_PMKID_CAND_PREAUTH;
  2008. pcand.index = i;
  2009. memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
  2010. memset(&wrqu, 0, sizeof(wrqu));
  2011. wrqu.data.length = sizeof(pcand);
  2012. wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
  2013. (u8 *)&pcand);
  2014. }
  2015. }
  2016. static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
  2017. struct rndis_indicate *msg, int buflen)
  2018. {
  2019. struct ndis_80211_status_indication *indication;
  2020. int len, offset;
  2021. offset = offsetof(struct rndis_indicate, status) +
  2022. le32_to_cpu(msg->offset);
  2023. len = le32_to_cpu(msg->length);
  2024. if (len < 8) {
  2025. devinfo(usbdev, "media specific indication, "
  2026. "ignore too short message (%i < 8)", len);
  2027. return;
  2028. }
  2029. if (offset + len > buflen) {
  2030. devinfo(usbdev, "media specific indication, "
  2031. "too large to fit to buffer (%i > %i)",
  2032. offset + len, buflen);
  2033. return;
  2034. }
  2035. indication = (void *)((u8 *)msg + offset);
  2036. switch (le32_to_cpu(indication->status_type)) {
  2037. case NDIS_80211_STATUSTYPE_RADIOSTATE:
  2038. devinfo(usbdev, "radio state indication: %i",
  2039. le32_to_cpu(indication->u.radio_status));
  2040. return;
  2041. case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
  2042. devinfo(usbdev, "media stream mode indication: %i",
  2043. le32_to_cpu(indication->u.media_stream_mode));
  2044. return;
  2045. case NDIS_80211_STATUSTYPE_AUTHENTICATION:
  2046. rndis_wlan_auth_indication(usbdev, indication, len);
  2047. return;
  2048. case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
  2049. rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
  2050. return;
  2051. default:
  2052. devinfo(usbdev, "media specific indication: "
  2053. "unknown status type 0x%08x",
  2054. le32_to_cpu(indication->status_type));
  2055. }
  2056. }
  2057. static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
  2058. {
  2059. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2060. struct rndis_indicate *msg = ind;
  2061. switch (msg->status) {
  2062. case RNDIS_STATUS_MEDIA_CONNECT:
  2063. devinfo(usbdev, "media connect");
  2064. /* queue work to avoid recursive calls into rndis_command */
  2065. set_bit(WORK_LINK_UP, &priv->work_pending);
  2066. queue_work(priv->workqueue, &priv->work);
  2067. break;
  2068. case RNDIS_STATUS_MEDIA_DISCONNECT:
  2069. devinfo(usbdev, "media disconnect");
  2070. /* queue work to avoid recursive calls into rndis_command */
  2071. set_bit(WORK_LINK_DOWN, &priv->work_pending);
  2072. queue_work(priv->workqueue, &priv->work);
  2073. break;
  2074. case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
  2075. rndis_wlan_media_specific_indication(usbdev, msg, buflen);
  2076. break;
  2077. default:
  2078. devinfo(usbdev, "indication: 0x%08x",
  2079. le32_to_cpu(msg->status));
  2080. break;
  2081. }
  2082. }
  2083. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  2084. {
  2085. struct {
  2086. __le32 num_items;
  2087. __le32 items[8];
  2088. } networks_supported;
  2089. int len, retval, i, n;
  2090. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2091. /* determine supported modes */
  2092. len = sizeof(networks_supported);
  2093. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  2094. &networks_supported, &len);
  2095. if (retval >= 0) {
  2096. n = le32_to_cpu(networks_supported.num_items);
  2097. if (n > 8)
  2098. n = 8;
  2099. for (i = 0; i < n; i++) {
  2100. switch (le32_to_cpu(networks_supported.items[i])) {
  2101. case NDIS_80211_TYPE_FREQ_HOP:
  2102. case NDIS_80211_TYPE_DIRECT_SEQ:
  2103. priv->caps |= CAP_MODE_80211B;
  2104. break;
  2105. case NDIS_80211_TYPE_OFDM_A:
  2106. priv->caps |= CAP_MODE_80211A;
  2107. break;
  2108. case NDIS_80211_TYPE_OFDM_G:
  2109. priv->caps |= CAP_MODE_80211G;
  2110. break;
  2111. }
  2112. }
  2113. }
  2114. return retval;
  2115. }
  2116. #define STATS_UPDATE_JIFFIES (HZ)
  2117. static void rndis_update_wireless_stats(struct work_struct *work)
  2118. {
  2119. struct rndis_wlan_private *priv =
  2120. container_of(work, struct rndis_wlan_private, stats_work.work);
  2121. struct usbnet *usbdev = priv->usbdev;
  2122. struct iw_statistics iwstats;
  2123. __le32 rssi, tmp;
  2124. int len, ret, j;
  2125. unsigned long flags;
  2126. int update_jiffies = STATS_UPDATE_JIFFIES;
  2127. void *buf;
  2128. spin_lock_irqsave(&priv->stats_lock, flags);
  2129. memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
  2130. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2131. /* only update stats when connected */
  2132. if (!is_associated(usbdev)) {
  2133. iwstats.qual.qual = 0;
  2134. iwstats.qual.level = 0;
  2135. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2136. | IW_QUAL_LEVEL_UPDATED
  2137. | IW_QUAL_NOISE_INVALID
  2138. | IW_QUAL_QUAL_INVALID
  2139. | IW_QUAL_LEVEL_INVALID;
  2140. goto end;
  2141. }
  2142. len = sizeof(rssi);
  2143. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  2144. devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
  2145. le32_to_cpu(rssi));
  2146. if (ret == 0) {
  2147. memset(&iwstats.qual, 0, sizeof(iwstats.qual));
  2148. iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
  2149. iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
  2150. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2151. | IW_QUAL_LEVEL_UPDATED
  2152. | IW_QUAL_NOISE_INVALID;
  2153. }
  2154. memset(&iwstats.discard, 0, sizeof(iwstats.discard));
  2155. len = sizeof(tmp);
  2156. ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
  2157. if (ret == 0)
  2158. iwstats.discard.misc += le32_to_cpu(tmp);
  2159. len = sizeof(tmp);
  2160. ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
  2161. if (ret == 0)
  2162. iwstats.discard.misc += le32_to_cpu(tmp);
  2163. len = sizeof(tmp);
  2164. ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
  2165. if (ret == 0)
  2166. iwstats.discard.misc += le32_to_cpu(tmp);
  2167. /* Workaround transfer stalls on poor quality links.
  2168. * TODO: find right way to fix these stalls (as stalls do not happen
  2169. * with ndiswrapper/windows driver). */
  2170. if (iwstats.qual.qual <= 25) {
  2171. /* Decrease stats worker interval to catch stalls.
  2172. * faster. Faster than 400-500ms causes packet loss,
  2173. * Slower doesn't catch stalls fast enough.
  2174. */
  2175. j = msecs_to_jiffies(priv->param_workaround_interval);
  2176. if (j > STATS_UPDATE_JIFFIES)
  2177. j = STATS_UPDATE_JIFFIES;
  2178. else if (j <= 0)
  2179. j = 1;
  2180. update_jiffies = j;
  2181. /* Send scan OID. Use of both OIDs is required to get device
  2182. * working.
  2183. */
  2184. tmp = cpu_to_le32(1);
  2185. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  2186. sizeof(tmp));
  2187. len = CONTROL_BUFFER_SIZE;
  2188. buf = kmalloc(len, GFP_KERNEL);
  2189. if (!buf)
  2190. goto end;
  2191. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  2192. kfree(buf);
  2193. }
  2194. end:
  2195. spin_lock_irqsave(&priv->stats_lock, flags);
  2196. memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
  2197. spin_unlock_irqrestore(&priv->stats_lock, flags);
  2198. if (update_jiffies >= HZ)
  2199. update_jiffies = round_jiffies_relative(update_jiffies);
  2200. else {
  2201. j = round_jiffies_relative(update_jiffies);
  2202. if (abs(j - update_jiffies) <= 10)
  2203. update_jiffies = j;
  2204. }
  2205. queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
  2206. }
  2207. static int bcm4320a_early_init(struct usbnet *usbdev)
  2208. {
  2209. /* bcm4320a doesn't handle configuration parameters well. Try
  2210. * set any and you get partially zeroed mac and broken device.
  2211. */
  2212. return 0;
  2213. }
  2214. static int bcm4320b_early_init(struct usbnet *usbdev)
  2215. {
  2216. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2217. char buf[8];
  2218. /* Early initialization settings, setting these won't have effect
  2219. * if called after generic_rndis_bind().
  2220. */
  2221. priv->param_country[0] = modparam_country[0];
  2222. priv->param_country[1] = modparam_country[1];
  2223. priv->param_country[2] = 0;
  2224. priv->param_frameburst = modparam_frameburst;
  2225. priv->param_afterburner = modparam_afterburner;
  2226. priv->param_power_save = modparam_power_save;
  2227. priv->param_power_output = modparam_power_output;
  2228. priv->param_roamtrigger = modparam_roamtrigger;
  2229. priv->param_roamdelta = modparam_roamdelta;
  2230. priv->param_country[0] = toupper(priv->param_country[0]);
  2231. priv->param_country[1] = toupper(priv->param_country[1]);
  2232. /* doesn't support EU as country code, use FI instead */
  2233. if (!strcmp(priv->param_country, "EU"))
  2234. strcpy(priv->param_country, "FI");
  2235. if (priv->param_power_save < 0)
  2236. priv->param_power_save = 0;
  2237. else if (priv->param_power_save > 2)
  2238. priv->param_power_save = 2;
  2239. if (priv->param_power_output < 0)
  2240. priv->param_power_output = 0;
  2241. else if (priv->param_power_output > 3)
  2242. priv->param_power_output = 3;
  2243. if (priv->param_roamtrigger < -80)
  2244. priv->param_roamtrigger = -80;
  2245. else if (priv->param_roamtrigger > -60)
  2246. priv->param_roamtrigger = -60;
  2247. if (priv->param_roamdelta < 0)
  2248. priv->param_roamdelta = 0;
  2249. else if (priv->param_roamdelta > 2)
  2250. priv->param_roamdelta = 2;
  2251. if (modparam_workaround_interval < 0)
  2252. priv->param_workaround_interval = 500;
  2253. else
  2254. priv->param_workaround_interval = modparam_workaround_interval;
  2255. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  2256. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  2257. priv->param_frameburst ? "1" : "0");
  2258. rndis_set_config_parameter_str(usbdev, "Afterburner",
  2259. priv->param_afterburner ? "1" : "0");
  2260. sprintf(buf, "%d", priv->param_power_save);
  2261. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  2262. sprintf(buf, "%d", priv->param_power_output);
  2263. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  2264. sprintf(buf, "%d", priv->param_roamtrigger);
  2265. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  2266. sprintf(buf, "%d", priv->param_roamdelta);
  2267. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  2268. return 0;
  2269. }
  2270. /* same as rndis_netdev_ops but with local multicast handler */
  2271. static const struct net_device_ops rndis_wlan_netdev_ops = {
  2272. .ndo_open = usbnet_open,
  2273. .ndo_stop = usbnet_stop,
  2274. .ndo_start_xmit = usbnet_start_xmit,
  2275. .ndo_tx_timeout = usbnet_tx_timeout,
  2276. .ndo_set_mac_address = eth_mac_addr,
  2277. .ndo_validate_addr = eth_validate_addr,
  2278. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  2279. };
  2280. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  2281. {
  2282. struct wiphy *wiphy;
  2283. struct rndis_wlan_private *priv;
  2284. int retval, len;
  2285. __le32 tmp;
  2286. /* allocate wiphy and rndis private data
  2287. * NOTE: We only support a single virtual interface, so wiphy
  2288. * and wireless_dev are somewhat synonymous for this device.
  2289. */
  2290. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  2291. if (!wiphy)
  2292. return -ENOMEM;
  2293. priv = wiphy_priv(wiphy);
  2294. usbdev->net->ieee80211_ptr = &priv->wdev;
  2295. priv->wdev.wiphy = wiphy;
  2296. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  2297. /* These have to be initialized before calling generic_rndis_bind().
  2298. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  2299. */
  2300. usbdev->driver_priv = priv;
  2301. usbdev->net->wireless_handlers = &rndis_iw_handlers;
  2302. priv->usbdev = usbdev;
  2303. mutex_init(&priv->command_lock);
  2304. spin_lock_init(&priv->stats_lock);
  2305. /* because rndis_command() sleeps we need to use workqueue */
  2306. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  2307. INIT_WORK(&priv->work, rndis_wlan_worker);
  2308. INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
  2309. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  2310. /* try bind rndis_host */
  2311. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  2312. if (retval < 0)
  2313. goto fail;
  2314. /* generic_rndis_bind set packet filter to multicast_all+
  2315. * promisc mode which doesn't work well for our devices (device
  2316. * picks up rssi to closest station instead of to access point).
  2317. *
  2318. * rndis_host wants to avoid all OID as much as possible
  2319. * so do promisc/multicast handling in rndis_wlan.
  2320. */
  2321. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  2322. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  2323. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  2324. sizeof(tmp));
  2325. len = sizeof(tmp);
  2326. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  2327. &len);
  2328. priv->multicast_size = le32_to_cpu(tmp);
  2329. if (retval < 0 || priv->multicast_size < 0)
  2330. priv->multicast_size = 0;
  2331. if (priv->multicast_size > 0)
  2332. usbdev->net->flags |= IFF_MULTICAST;
  2333. else
  2334. usbdev->net->flags &= ~IFF_MULTICAST;
  2335. priv->iwstats.qual.qual = 0;
  2336. priv->iwstats.qual.level = 0;
  2337. priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  2338. | IW_QUAL_LEVEL_UPDATED
  2339. | IW_QUAL_NOISE_INVALID
  2340. | IW_QUAL_QUAL_INVALID
  2341. | IW_QUAL_LEVEL_INVALID;
  2342. /* fill-out wiphy structure and register w/ cfg80211 */
  2343. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2344. wiphy->privid = rndis_wiphy_privid;
  2345. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2346. | BIT(NL80211_IFTYPE_ADHOC);
  2347. wiphy->max_scan_ssids = 1;
  2348. /* TODO: fill-out band information based on priv->caps */
  2349. rndis_wlan_get_caps(usbdev);
  2350. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2351. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2352. priv->band.channels = priv->channels;
  2353. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2354. priv->band.bitrates = priv->rates;
  2355. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2356. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2357. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2358. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2359. if (wiphy_register(wiphy)) {
  2360. retval = -ENODEV;
  2361. goto fail;
  2362. }
  2363. set_default_iw_params(usbdev);
  2364. /* set default rts/frag */
  2365. rndis_set_wiphy_params(wiphy,
  2366. WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
  2367. /* turn radio on */
  2368. priv->radio_on = 1;
  2369. disassociate(usbdev, 1);
  2370. netif_carrier_off(usbdev->net);
  2371. return 0;
  2372. fail:
  2373. cancel_delayed_work_sync(&priv->stats_work);
  2374. cancel_delayed_work_sync(&priv->scan_work);
  2375. cancel_work_sync(&priv->work);
  2376. flush_workqueue(priv->workqueue);
  2377. destroy_workqueue(priv->workqueue);
  2378. wiphy_free(wiphy);
  2379. return retval;
  2380. }
  2381. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2382. {
  2383. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2384. /* turn radio off */
  2385. disassociate(usbdev, 0);
  2386. cancel_delayed_work_sync(&priv->stats_work);
  2387. cancel_delayed_work_sync(&priv->scan_work);
  2388. cancel_work_sync(&priv->work);
  2389. flush_workqueue(priv->workqueue);
  2390. destroy_workqueue(priv->workqueue);
  2391. if (priv && priv->wpa_ie_len)
  2392. kfree(priv->wpa_ie);
  2393. rndis_unbind(usbdev, intf);
  2394. wiphy_unregister(priv->wdev.wiphy);
  2395. wiphy_free(priv->wdev.wiphy);
  2396. }
  2397. static int rndis_wlan_reset(struct usbnet *usbdev)
  2398. {
  2399. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2400. int retval;
  2401. devdbg(usbdev, "rndis_wlan_reset");
  2402. retval = rndis_reset(usbdev);
  2403. if (retval)
  2404. devwarn(usbdev, "rndis_reset() failed: %d", retval);
  2405. /* rndis_reset cleared multicast list, so restore here.
  2406. (set_multicast_list() also turns on current packet filter) */
  2407. set_multicast_list(usbdev);
  2408. queue_delayed_work(priv->workqueue, &priv->stats_work,
  2409. round_jiffies_relative(STATS_UPDATE_JIFFIES));
  2410. return deauthenticate(usbdev);
  2411. }
  2412. static int rndis_wlan_stop(struct usbnet *usbdev)
  2413. {
  2414. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2415. int retval;
  2416. __le32 filter;
  2417. devdbg(usbdev, "rndis_wlan_stop");
  2418. retval = disassociate(usbdev, 0);
  2419. priv->work_pending = 0;
  2420. cancel_delayed_work_sync(&priv->stats_work);
  2421. cancel_delayed_work_sync(&priv->scan_work);
  2422. cancel_work_sync(&priv->work);
  2423. flush_workqueue(priv->workqueue);
  2424. if (priv->scan_request) {
  2425. cfg80211_scan_done(priv->scan_request, true);
  2426. priv->scan_request = NULL;
  2427. }
  2428. /* Set current packet filter zero to block receiving data packets from
  2429. device. */
  2430. filter = 0;
  2431. rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  2432. sizeof(filter));
  2433. return retval;
  2434. }
  2435. static const struct driver_info bcm4320b_info = {
  2436. .description = "Wireless RNDIS device, BCM4320b based",
  2437. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2438. FLAG_AVOID_UNLINK_URBS,
  2439. .bind = rndis_wlan_bind,
  2440. .unbind = rndis_wlan_unbind,
  2441. .status = rndis_status,
  2442. .rx_fixup = rndis_rx_fixup,
  2443. .tx_fixup = rndis_tx_fixup,
  2444. .reset = rndis_wlan_reset,
  2445. .stop = rndis_wlan_stop,
  2446. .early_init = bcm4320b_early_init,
  2447. .indication = rndis_wlan_indication,
  2448. };
  2449. static const struct driver_info bcm4320a_info = {
  2450. .description = "Wireless RNDIS device, BCM4320a based",
  2451. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2452. FLAG_AVOID_UNLINK_URBS,
  2453. .bind = rndis_wlan_bind,
  2454. .unbind = rndis_wlan_unbind,
  2455. .status = rndis_status,
  2456. .rx_fixup = rndis_rx_fixup,
  2457. .tx_fixup = rndis_tx_fixup,
  2458. .reset = rndis_wlan_reset,
  2459. .stop = rndis_wlan_stop,
  2460. .early_init = bcm4320a_early_init,
  2461. .indication = rndis_wlan_indication,
  2462. };
  2463. static const struct driver_info rndis_wlan_info = {
  2464. .description = "Wireless RNDIS device",
  2465. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
  2466. FLAG_AVOID_UNLINK_URBS,
  2467. .bind = rndis_wlan_bind,
  2468. .unbind = rndis_wlan_unbind,
  2469. .status = rndis_status,
  2470. .rx_fixup = rndis_rx_fixup,
  2471. .tx_fixup = rndis_tx_fixup,
  2472. .reset = rndis_wlan_reset,
  2473. .stop = rndis_wlan_stop,
  2474. .early_init = bcm4320a_early_init,
  2475. .indication = rndis_wlan_indication,
  2476. };
  2477. /*-------------------------------------------------------------------------*/
  2478. static const struct usb_device_id products [] = {
  2479. #define RNDIS_MASTER_INTERFACE \
  2480. .bInterfaceClass = USB_CLASS_COMM, \
  2481. .bInterfaceSubClass = 2 /* ACM */, \
  2482. .bInterfaceProtocol = 0x0ff
  2483. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2484. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2485. */
  2486. {
  2487. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2488. | USB_DEVICE_ID_MATCH_DEVICE,
  2489. .idVendor = 0x0411,
  2490. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2491. RNDIS_MASTER_INTERFACE,
  2492. .driver_info = (unsigned long) &bcm4320b_info,
  2493. }, {
  2494. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2495. | USB_DEVICE_ID_MATCH_DEVICE,
  2496. .idVendor = 0x0baf,
  2497. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2498. RNDIS_MASTER_INTERFACE,
  2499. .driver_info = (unsigned long) &bcm4320b_info,
  2500. }, {
  2501. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2502. | USB_DEVICE_ID_MATCH_DEVICE,
  2503. .idVendor = 0x050d,
  2504. .idProduct = 0x011b, /* Belkin F5D7051 */
  2505. RNDIS_MASTER_INTERFACE,
  2506. .driver_info = (unsigned long) &bcm4320b_info,
  2507. }, {
  2508. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2509. | USB_DEVICE_ID_MATCH_DEVICE,
  2510. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2511. .idProduct = 0x011b, /* Belkin F5D7051 */
  2512. RNDIS_MASTER_INTERFACE,
  2513. .driver_info = (unsigned long) &bcm4320b_info,
  2514. }, {
  2515. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2516. | USB_DEVICE_ID_MATCH_DEVICE,
  2517. .idVendor = 0x13b1,
  2518. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2519. RNDIS_MASTER_INTERFACE,
  2520. .driver_info = (unsigned long) &bcm4320b_info,
  2521. }, {
  2522. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2523. | USB_DEVICE_ID_MATCH_DEVICE,
  2524. .idVendor = 0x13b1,
  2525. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2526. RNDIS_MASTER_INTERFACE,
  2527. .driver_info = (unsigned long) &bcm4320b_info,
  2528. }, {
  2529. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2530. | USB_DEVICE_ID_MATCH_DEVICE,
  2531. .idVendor = 0x0b05,
  2532. .idProduct = 0x1717, /* Asus WL169gE */
  2533. RNDIS_MASTER_INTERFACE,
  2534. .driver_info = (unsigned long) &bcm4320b_info,
  2535. }, {
  2536. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2537. | USB_DEVICE_ID_MATCH_DEVICE,
  2538. .idVendor = 0x0a5c,
  2539. .idProduct = 0xd11b, /* Eminent EM4045 */
  2540. RNDIS_MASTER_INTERFACE,
  2541. .driver_info = (unsigned long) &bcm4320b_info,
  2542. }, {
  2543. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2544. | USB_DEVICE_ID_MATCH_DEVICE,
  2545. .idVendor = 0x1690,
  2546. .idProduct = 0x0715, /* BT Voyager 1055 */
  2547. RNDIS_MASTER_INTERFACE,
  2548. .driver_info = (unsigned long) &bcm4320b_info,
  2549. },
  2550. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2551. * parameters available, hardware probably contain older firmware version with
  2552. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2553. */
  2554. {
  2555. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2556. | USB_DEVICE_ID_MATCH_DEVICE,
  2557. .idVendor = 0x13b1,
  2558. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2559. RNDIS_MASTER_INTERFACE,
  2560. .driver_info = (unsigned long) &bcm4320a_info,
  2561. }, {
  2562. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2563. | USB_DEVICE_ID_MATCH_DEVICE,
  2564. .idVendor = 0x0baf,
  2565. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2566. RNDIS_MASTER_INTERFACE,
  2567. .driver_info = (unsigned long) &bcm4320a_info,
  2568. }, {
  2569. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2570. | USB_DEVICE_ID_MATCH_DEVICE,
  2571. .idVendor = 0x0411,
  2572. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2573. RNDIS_MASTER_INTERFACE,
  2574. .driver_info = (unsigned long) &bcm4320a_info,
  2575. },
  2576. /* Generic Wireless RNDIS devices that we don't have exact
  2577. * idVendor/idProduct/chip yet.
  2578. */
  2579. {
  2580. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2581. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2582. .driver_info = (unsigned long) &rndis_wlan_info,
  2583. }, {
  2584. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2585. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2586. .driver_info = (unsigned long) &rndis_wlan_info,
  2587. },
  2588. { }, // END
  2589. };
  2590. MODULE_DEVICE_TABLE(usb, products);
  2591. static struct usb_driver rndis_wlan_driver = {
  2592. .name = "rndis_wlan",
  2593. .id_table = products,
  2594. .probe = usbnet_probe,
  2595. .disconnect = usbnet_disconnect,
  2596. .suspend = usbnet_suspend,
  2597. .resume = usbnet_resume,
  2598. };
  2599. static int __init rndis_wlan_init(void)
  2600. {
  2601. return usb_register(&rndis_wlan_driver);
  2602. }
  2603. module_init(rndis_wlan_init);
  2604. static void __exit rndis_wlan_exit(void)
  2605. {
  2606. usb_deregister(&rndis_wlan_driver);
  2607. }
  2608. module_exit(rndis_wlan_exit);
  2609. MODULE_AUTHOR("Bjorge Dijkstra");
  2610. MODULE_AUTHOR("Jussi Kivilinna");
  2611. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2612. MODULE_LICENSE("GPL");