rndis_wlan.c 85 KB

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