rndis_wlan.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  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_PERMANENT_ADDRESS cpu_to_le32(0x01010101)
  89. #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
  90. #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
  91. #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
  92. #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
  93. #define OID_802_11_SSID cpu_to_le32(0x0d010102)
  94. #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
  95. #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
  96. #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
  97. #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
  98. #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
  99. #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
  100. #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
  101. #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
  102. #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
  103. #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
  104. #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
  105. #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
  106. #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
  107. #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
  108. #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
  109. #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
  110. #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
  111. #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
  112. #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
  113. #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
  114. #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
  115. #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
  116. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  117. #define WL_NOISE -96 /* typical noise level in dBm */
  118. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  119. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  120. * based on wireless rndis) has default txpower of 13dBm.
  121. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  122. * 13dBm == 19.9mW
  123. */
  124. #define BCM4320_DEFAULT_TXPOWER 20
  125. /* codes for "status" field of completion messages */
  126. #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
  127. #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
  128. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  129. * slightly modified for datatype endianess, etc
  130. */
  131. #define NDIS_802_11_LENGTH_SSID 32
  132. #define NDIS_802_11_LENGTH_RATES 8
  133. #define NDIS_802_11_LENGTH_RATES_EX 16
  134. enum ndis_80211_net_type {
  135. NDIS_80211_TYPE_FREQ_HOP,
  136. NDIS_80211_TYPE_DIRECT_SEQ,
  137. NDIS_80211_TYPE_OFDM_A,
  138. NDIS_80211_TYPE_OFDM_G
  139. };
  140. enum ndis_80211_net_infra {
  141. NDIS_80211_INFRA_ADHOC,
  142. NDIS_80211_INFRA_INFRA,
  143. NDIS_80211_INFRA_AUTO_UNKNOWN
  144. };
  145. enum ndis_80211_auth_mode {
  146. NDIS_80211_AUTH_OPEN,
  147. NDIS_80211_AUTH_SHARED,
  148. NDIS_80211_AUTH_AUTO_SWITCH,
  149. NDIS_80211_AUTH_WPA,
  150. NDIS_80211_AUTH_WPA_PSK,
  151. NDIS_80211_AUTH_WPA_NONE,
  152. NDIS_80211_AUTH_WPA2,
  153. NDIS_80211_AUTH_WPA2_PSK
  154. };
  155. enum ndis_80211_encr_status {
  156. NDIS_80211_ENCR_WEP_ENABLED,
  157. NDIS_80211_ENCR_DISABLED,
  158. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  159. NDIS_80211_ENCR_NOT_SUPPORTED,
  160. NDIS_80211_ENCR_TKIP_ENABLED,
  161. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  162. NDIS_80211_ENCR_CCMP_ENABLED,
  163. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  164. };
  165. enum ndis_80211_priv_filter {
  166. NDIS_80211_PRIV_ACCEPT_ALL,
  167. NDIS_80211_PRIV_8021X_WEP
  168. };
  169. enum ndis_80211_addkey_bits {
  170. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  171. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  172. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  173. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  174. };
  175. enum ndis_80211_addwep_bits {
  176. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  177. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  178. };
  179. struct ndis_80211_ssid {
  180. __le32 length;
  181. u8 essid[NDIS_802_11_LENGTH_SSID];
  182. } __attribute__((packed));
  183. struct ndis_80211_conf_freq_hop {
  184. __le32 length;
  185. __le32 hop_pattern;
  186. __le32 hop_set;
  187. __le32 dwell_time;
  188. } __attribute__((packed));
  189. struct ndis_80211_conf {
  190. __le32 length;
  191. __le32 beacon_period;
  192. __le32 atim_window;
  193. __le32 ds_config;
  194. struct ndis_80211_conf_freq_hop fh_config;
  195. } __attribute__((packed));
  196. struct ndis_80211_bssid_ex {
  197. __le32 length;
  198. u8 mac[6];
  199. u8 padding[2];
  200. struct ndis_80211_ssid ssid;
  201. __le32 privacy;
  202. __le32 rssi;
  203. __le32 net_type;
  204. struct ndis_80211_conf config;
  205. __le32 net_infra;
  206. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  207. __le32 ie_length;
  208. u8 ies[0];
  209. } __attribute__((packed));
  210. struct ndis_80211_bssid_list_ex {
  211. __le32 num_items;
  212. struct ndis_80211_bssid_ex bssid[0];
  213. } __attribute__((packed));
  214. struct ndis_80211_fixed_ies {
  215. u8 timestamp[8];
  216. __le16 beacon_interval;
  217. __le16 capabilities;
  218. } __attribute__((packed));
  219. struct ndis_80211_wep_key {
  220. __le32 size;
  221. __le32 index;
  222. __le32 length;
  223. u8 material[32];
  224. } __attribute__((packed));
  225. struct ndis_80211_key {
  226. __le32 size;
  227. __le32 index;
  228. __le32 length;
  229. u8 bssid[6];
  230. u8 padding[6];
  231. u8 rsc[8];
  232. u8 material[32];
  233. } __attribute__((packed));
  234. struct ndis_80211_remove_key {
  235. __le32 size;
  236. __le32 index;
  237. u8 bssid[6];
  238. } __attribute__((packed));
  239. struct ndis_config_param {
  240. __le32 name_offs;
  241. __le32 name_length;
  242. __le32 type;
  243. __le32 value_offs;
  244. __le32 value_length;
  245. } __attribute__((packed));
  246. struct ndis_80211_assoc_info {
  247. __le32 length;
  248. __le16 req_ies;
  249. struct req_ie {
  250. __le16 capa;
  251. __le16 listen_interval;
  252. u8 cur_ap_address[6];
  253. } req_ie;
  254. __le32 req_ie_length;
  255. __le32 offset_req_ies;
  256. __le16 resp_ies;
  257. struct resp_ie {
  258. __le16 capa;
  259. __le16 status_code;
  260. __le16 assoc_id;
  261. } resp_ie;
  262. __le32 resp_ie_length;
  263. __le32 offset_resp_ies;
  264. } __attribute__((packed));
  265. /* these have to match what is in wpa_supplicant */
  266. enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
  267. enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
  268. CIPHER_WEP104 };
  269. enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
  270. KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
  271. /*
  272. * private data
  273. */
  274. #define NET_TYPE_11FB 0
  275. #define CAP_MODE_80211A 1
  276. #define CAP_MODE_80211B 2
  277. #define CAP_MODE_80211G 4
  278. #define CAP_MODE_MASK 7
  279. #define WORK_LINK_UP (1<<0)
  280. #define WORK_LINK_DOWN (1<<1)
  281. #define WORK_SET_MULTICAST_LIST (1<<2)
  282. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  283. static const struct ieee80211_channel rndis_channels[] = {
  284. { .center_freq = 2412 },
  285. { .center_freq = 2417 },
  286. { .center_freq = 2422 },
  287. { .center_freq = 2427 },
  288. { .center_freq = 2432 },
  289. { .center_freq = 2437 },
  290. { .center_freq = 2442 },
  291. { .center_freq = 2447 },
  292. { .center_freq = 2452 },
  293. { .center_freq = 2457 },
  294. { .center_freq = 2462 },
  295. { .center_freq = 2467 },
  296. { .center_freq = 2472 },
  297. { .center_freq = 2484 },
  298. };
  299. static const struct ieee80211_rate rndis_rates[] = {
  300. { .bitrate = 10 },
  301. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  302. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  303. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  304. { .bitrate = 60 },
  305. { .bitrate = 90 },
  306. { .bitrate = 120 },
  307. { .bitrate = 180 },
  308. { .bitrate = 240 },
  309. { .bitrate = 360 },
  310. { .bitrate = 480 },
  311. { .bitrate = 540 }
  312. };
  313. /* RNDIS device private data */
  314. struct rndis_wlan_private {
  315. struct usbnet *usbdev;
  316. struct wireless_dev wdev;
  317. struct cfg80211_scan_request *scan_request;
  318. struct workqueue_struct *workqueue;
  319. struct delayed_work stats_work;
  320. struct delayed_work scan_work;
  321. struct work_struct work;
  322. struct mutex command_lock;
  323. spinlock_t stats_lock;
  324. unsigned long work_pending;
  325. struct ieee80211_supported_band band;
  326. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  327. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  328. struct iw_statistics iwstats;
  329. struct iw_statistics privstats;
  330. int caps;
  331. int multicast_size;
  332. /* module parameters */
  333. char param_country[4];
  334. int param_frameburst;
  335. int param_afterburner;
  336. int param_power_save;
  337. int param_power_output;
  338. int param_roamtrigger;
  339. int param_roamdelta;
  340. u32 param_workaround_interval;
  341. /* hardware state */
  342. int radio_on;
  343. int infra_mode;
  344. struct ndis_80211_ssid essid;
  345. /* encryption stuff */
  346. int encr_tx_key_index;
  347. char encr_keys[4][32];
  348. int encr_key_len[4];
  349. char encr_key_wpa[4];
  350. int wpa_version;
  351. int wpa_keymgmt;
  352. int wpa_authalg;
  353. int wpa_ie_len;
  354. u8 *wpa_ie;
  355. int wpa_cipher_pair;
  356. int wpa_cipher_group;
  357. u8 command_buffer[COMMAND_BUFFER_SIZE];
  358. };
  359. /*
  360. * cfg80211 ops
  361. */
  362. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  363. struct net_device *dev,
  364. enum nl80211_iftype type, u32 *flags,
  365. struct vif_params *params);
  366. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  367. struct cfg80211_scan_request *request);
  368. static struct cfg80211_ops rndis_config_ops = {
  369. .change_virtual_intf = rndis_change_virtual_intf,
  370. .scan = rndis_scan,
  371. };
  372. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  373. static const int bcm4320_power_output[4] = { 25, 50, 75, 100 };
  374. static const unsigned char zero_bssid[ETH_ALEN] = {0,};
  375. static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
  376. 0xff, 0xff, 0xff };
  377. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  378. {
  379. return (struct rndis_wlan_private *)dev->driver_priv;
  380. }
  381. static u32 get_bcm4320_power(struct rndis_wlan_private *priv)
  382. {
  383. return BCM4320_DEFAULT_TXPOWER *
  384. bcm4320_power_output[priv->param_power_output] / 100;
  385. }
  386. /* translate error code */
  387. static int rndis_error_status(__le32 rndis_status)
  388. {
  389. int ret = -EINVAL;
  390. switch (rndis_status) {
  391. case RNDIS_STATUS_SUCCESS:
  392. ret = 0;
  393. break;
  394. case RNDIS_STATUS_FAILURE:
  395. case RNDIS_STATUS_INVALID_DATA:
  396. ret = -EINVAL;
  397. break;
  398. case RNDIS_STATUS_NOT_SUPPORTED:
  399. ret = -EOPNOTSUPP;
  400. break;
  401. case RNDIS_STATUS_ADAPTER_NOT_READY:
  402. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  403. ret = -EBUSY;
  404. break;
  405. }
  406. return ret;
  407. }
  408. static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
  409. {
  410. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  411. union {
  412. void *buf;
  413. struct rndis_msg_hdr *header;
  414. struct rndis_query *get;
  415. struct rndis_query_c *get_c;
  416. } u;
  417. int ret, buflen;
  418. buflen = *len + sizeof(*u.get);
  419. if (buflen < CONTROL_BUFFER_SIZE)
  420. buflen = CONTROL_BUFFER_SIZE;
  421. if (buflen > COMMAND_BUFFER_SIZE) {
  422. u.buf = kmalloc(buflen, GFP_KERNEL);
  423. if (!u.buf)
  424. return -ENOMEM;
  425. } else {
  426. u.buf = priv->command_buffer;
  427. }
  428. mutex_lock(&priv->command_lock);
  429. memset(u.get, 0, sizeof *u.get);
  430. u.get->msg_type = RNDIS_MSG_QUERY;
  431. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  432. u.get->oid = oid;
  433. ret = rndis_command(dev, u.header, buflen);
  434. if (ret == 0) {
  435. ret = le32_to_cpu(u.get_c->len);
  436. *len = (*len > ret) ? ret : *len;
  437. memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
  438. ret = rndis_error_status(u.get_c->status);
  439. }
  440. mutex_unlock(&priv->command_lock);
  441. if (u.buf != priv->command_buffer)
  442. kfree(u.buf);
  443. return ret;
  444. }
  445. static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
  446. {
  447. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  448. union {
  449. void *buf;
  450. struct rndis_msg_hdr *header;
  451. struct rndis_set *set;
  452. struct rndis_set_c *set_c;
  453. } u;
  454. int ret, buflen;
  455. buflen = len + sizeof(*u.set);
  456. if (buflen < CONTROL_BUFFER_SIZE)
  457. buflen = CONTROL_BUFFER_SIZE;
  458. if (buflen > COMMAND_BUFFER_SIZE) {
  459. u.buf = kmalloc(buflen, GFP_KERNEL);
  460. if (!u.buf)
  461. return -ENOMEM;
  462. } else {
  463. u.buf = priv->command_buffer;
  464. }
  465. mutex_lock(&priv->command_lock);
  466. memset(u.set, 0, sizeof *u.set);
  467. u.set->msg_type = RNDIS_MSG_SET;
  468. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  469. u.set->oid = oid;
  470. u.set->len = cpu_to_le32(len);
  471. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  472. u.set->handle = cpu_to_le32(0);
  473. memcpy(u.buf + sizeof(*u.set), data, len);
  474. ret = rndis_command(dev, u.header, buflen);
  475. if (ret == 0)
  476. ret = rndis_error_status(u.set_c->status);
  477. mutex_unlock(&priv->command_lock);
  478. if (u.buf != priv->command_buffer)
  479. kfree(u.buf);
  480. return ret;
  481. }
  482. /*
  483. * Specs say that we can only set config parameters only soon after device
  484. * initialization.
  485. * value_type: 0 = u32, 2 = unicode string
  486. */
  487. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  488. int value_type, void *value)
  489. {
  490. struct ndis_config_param *infobuf;
  491. int value_len, info_len, param_len, ret, i;
  492. __le16 *unibuf;
  493. __le32 *dst_value;
  494. if (value_type == 0)
  495. value_len = sizeof(__le32);
  496. else if (value_type == 2)
  497. value_len = strlen(value) * sizeof(__le16);
  498. else
  499. return -EINVAL;
  500. param_len = strlen(param) * sizeof(__le16);
  501. info_len = sizeof(*infobuf) + param_len + value_len;
  502. #ifdef DEBUG
  503. info_len += 12;
  504. #endif
  505. infobuf = kmalloc(info_len, GFP_KERNEL);
  506. if (!infobuf)
  507. return -ENOMEM;
  508. #ifdef DEBUG
  509. info_len -= 12;
  510. /* extra 12 bytes are for padding (debug output) */
  511. memset(infobuf, 0xCC, info_len + 12);
  512. #endif
  513. if (value_type == 2)
  514. devdbg(dev, "setting config parameter: %s, value: %s",
  515. param, (u8 *)value);
  516. else
  517. devdbg(dev, "setting config parameter: %s, value: %d",
  518. param, *(u32 *)value);
  519. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  520. infobuf->name_length = cpu_to_le32(param_len);
  521. infobuf->type = cpu_to_le32(value_type);
  522. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  523. infobuf->value_length = cpu_to_le32(value_len);
  524. /* simple string to unicode string conversion */
  525. unibuf = (void *)infobuf + sizeof(*infobuf);
  526. for (i = 0; i < param_len / sizeof(__le16); i++)
  527. unibuf[i] = cpu_to_le16(param[i]);
  528. if (value_type == 2) {
  529. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  530. for (i = 0; i < value_len / sizeof(__le16); i++)
  531. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  532. } else {
  533. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  534. *dst_value = cpu_to_le32(*(u32 *)value);
  535. }
  536. #ifdef DEBUG
  537. devdbg(dev, "info buffer (len: %d):", info_len);
  538. for (i = 0; i < info_len; i += 12) {
  539. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  540. devdbg(dev, "%08X:%08X:%08X",
  541. cpu_to_be32(tmp[0]),
  542. cpu_to_be32(tmp[1]),
  543. cpu_to_be32(tmp[2]));
  544. }
  545. #endif
  546. ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
  547. infobuf, info_len);
  548. if (ret != 0)
  549. devdbg(dev, "setting rndis config parameter failed, %d.", ret);
  550. kfree(infobuf);
  551. return ret;
  552. }
  553. static int rndis_set_config_parameter_str(struct usbnet *dev,
  554. char *param, char *value)
  555. {
  556. return(rndis_set_config_parameter(dev, param, 2, value));
  557. }
  558. /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
  559. char *param, u32 value)
  560. {
  561. return(rndis_set_config_parameter(dev, param, 0, &value));
  562. }*/
  563. /*
  564. * data conversion functions
  565. */
  566. static int level_to_qual(int level)
  567. {
  568. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  569. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  570. }
  571. static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
  572. {
  573. freq->e = 0;
  574. freq->i = 0;
  575. freq->flags = 0;
  576. /* see comment in wireless.h above the "struct iw_freq"
  577. * definition for an explanation of this if
  578. * NOTE: 1000000 is due to the kHz
  579. */
  580. if (dsconfig > 1000000) {
  581. freq->m = dsconfig / 10;
  582. freq->e = 1;
  583. } else
  584. freq->m = dsconfig;
  585. /* convert from kHz to Hz */
  586. freq->e += 3;
  587. }
  588. static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
  589. {
  590. if (freq->m < 1000 && freq->e == 0) {
  591. if (freq->m >= 1 && freq->m <= 14)
  592. *dsconfig = ieee80211_dsss_chan_to_freq(freq->m) * 1000;
  593. else
  594. return -1;
  595. } else {
  596. int i;
  597. *dsconfig = freq->m;
  598. for (i = freq->e; i > 0; i--)
  599. *dsconfig *= 10;
  600. *dsconfig /= 1000;
  601. }
  602. return 0;
  603. }
  604. /*
  605. * common functions
  606. */
  607. static int
  608. add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index);
  609. static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  610. {
  611. int ret, len;
  612. len = sizeof(*ssid);
  613. ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
  614. if (ret != 0)
  615. ssid->length = 0;
  616. #ifdef DEBUG
  617. {
  618. unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
  619. memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
  620. tmp[le32_to_cpu(ssid->length)] = 0;
  621. devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
  622. }
  623. #endif
  624. return ret;
  625. }
  626. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  627. {
  628. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  629. int ret;
  630. ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
  631. if (ret == 0) {
  632. memcpy(&priv->essid, ssid, sizeof(priv->essid));
  633. priv->radio_on = 1;
  634. devdbg(usbdev, "set_essid: radio_on = 1");
  635. }
  636. return ret;
  637. }
  638. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  639. {
  640. int ret, len;
  641. len = ETH_ALEN;
  642. ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
  643. if (ret != 0)
  644. memset(bssid, 0, ETH_ALEN);
  645. return ret;
  646. }
  647. static int get_association_info(struct usbnet *usbdev,
  648. struct ndis_80211_assoc_info *info, int len)
  649. {
  650. return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
  651. info, &len);
  652. }
  653. static int is_associated(struct usbnet *usbdev)
  654. {
  655. u8 bssid[ETH_ALEN];
  656. int ret;
  657. ret = get_bssid(usbdev, bssid);
  658. return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
  659. }
  660. static int disassociate(struct usbnet *usbdev, int reset_ssid)
  661. {
  662. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  663. struct ndis_80211_ssid ssid;
  664. int i, ret = 0;
  665. if (priv->radio_on) {
  666. ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
  667. if (ret == 0) {
  668. priv->radio_on = 0;
  669. devdbg(usbdev, "disassociate: radio_on = 0");
  670. if (reset_ssid)
  671. msleep(100);
  672. }
  673. }
  674. /* disassociate causes radio to be turned off; if reset_ssid
  675. * is given, set random ssid to enable radio */
  676. if (reset_ssid) {
  677. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  678. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  679. ssid.essid[0] = 0x1;
  680. ssid.essid[1] = 0xff;
  681. for (i = 2; i < sizeof(ssid.essid); i++)
  682. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  683. ret = set_essid(usbdev, &ssid);
  684. }
  685. return ret;
  686. }
  687. static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
  688. {
  689. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  690. __le32 tmp;
  691. int auth_mode, ret;
  692. devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
  693. "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
  694. if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
  695. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  696. auth_mode = NDIS_80211_AUTH_WPA2;
  697. else
  698. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  699. } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
  700. if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
  701. auth_mode = NDIS_80211_AUTH_WPA;
  702. else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
  703. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  704. else
  705. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  706. } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
  707. if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
  708. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  709. else
  710. auth_mode = NDIS_80211_AUTH_SHARED;
  711. } else
  712. auth_mode = NDIS_80211_AUTH_OPEN;
  713. tmp = cpu_to_le32(auth_mode);
  714. ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
  715. sizeof(tmp));
  716. if (ret != 0) {
  717. devwarn(usbdev, "setting auth mode failed (%08X)", ret);
  718. return ret;
  719. }
  720. priv->wpa_version = wpa_version;
  721. priv->wpa_authalg = authalg;
  722. return 0;
  723. }
  724. static int set_priv_filter(struct usbnet *usbdev)
  725. {
  726. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  727. __le32 tmp;
  728. devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
  729. if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
  730. priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
  731. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  732. else
  733. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  734. return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
  735. sizeof(tmp));
  736. }
  737. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  738. {
  739. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  740. __le32 tmp;
  741. int encr_mode, ret;
  742. devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
  743. pairwise,
  744. groupwise);
  745. if (pairwise & IW_AUTH_CIPHER_CCMP)
  746. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  747. else if (pairwise & IW_AUTH_CIPHER_TKIP)
  748. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  749. else if (pairwise &
  750. (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
  751. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  752. else if (groupwise & IW_AUTH_CIPHER_CCMP)
  753. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  754. else if (groupwise & IW_AUTH_CIPHER_TKIP)
  755. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  756. else
  757. encr_mode = NDIS_80211_ENCR_DISABLED;
  758. tmp = cpu_to_le32(encr_mode);
  759. ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
  760. sizeof(tmp));
  761. if (ret != 0) {
  762. devwarn(usbdev, "setting encr mode failed (%08X)", ret);
  763. return ret;
  764. }
  765. priv->wpa_cipher_pair = pairwise;
  766. priv->wpa_cipher_group = groupwise;
  767. return 0;
  768. }
  769. static int set_assoc_params(struct usbnet *usbdev)
  770. {
  771. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  772. set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
  773. set_priv_filter(usbdev);
  774. set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
  775. return 0;
  776. }
  777. static int set_infra_mode(struct usbnet *usbdev, int mode)
  778. {
  779. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  780. __le32 tmp;
  781. int ret, i;
  782. devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
  783. tmp = cpu_to_le32(mode);
  784. ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
  785. sizeof(tmp));
  786. if (ret != 0) {
  787. devwarn(usbdev, "setting infra mode failed (%08X)", ret);
  788. return ret;
  789. }
  790. /* NDIS drivers clear keys when infrastructure mode is
  791. * changed. But Linux tools assume otherwise. So set the
  792. * keys */
  793. if (priv->wpa_keymgmt == 0 ||
  794. priv->wpa_keymgmt == IW_AUTH_KEY_MGMT_802_1X) {
  795. for (i = 0; i < 4; i++) {
  796. if (priv->encr_key_len[i] > 0 && !priv->encr_key_wpa[i])
  797. add_wep_key(usbdev, priv->encr_keys[i],
  798. priv->encr_key_len[i], i);
  799. }
  800. }
  801. priv->infra_mode = mode;
  802. return 0;
  803. }
  804. static void set_default_iw_params(struct usbnet *usbdev)
  805. {
  806. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  807. priv->wpa_keymgmt = 0;
  808. priv->wpa_version = 0;
  809. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  810. set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  811. IW_AUTH_ALG_OPEN_SYSTEM);
  812. set_priv_filter(usbdev);
  813. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  814. }
  815. static int deauthenticate(struct usbnet *usbdev)
  816. {
  817. int ret;
  818. ret = disassociate(usbdev, 1);
  819. set_default_iw_params(usbdev);
  820. return ret;
  821. }
  822. /* index must be 0 - N, as per NDIS */
  823. static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
  824. {
  825. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  826. struct ndis_80211_wep_key ndis_key;
  827. int ret;
  828. if (key_len <= 0 || key_len > 32 || index < 0 || index >= 4)
  829. return -EINVAL;
  830. memset(&ndis_key, 0, sizeof(ndis_key));
  831. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  832. ndis_key.length = cpu_to_le32(key_len);
  833. ndis_key.index = cpu_to_le32(index);
  834. memcpy(&ndis_key.material, key, key_len);
  835. if (index == priv->encr_tx_key_index) {
  836. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  837. ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
  838. IW_AUTH_CIPHER_NONE);
  839. if (ret)
  840. devwarn(usbdev, "encryption couldn't be enabled (%08X)",
  841. ret);
  842. }
  843. ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
  844. sizeof(ndis_key));
  845. if (ret != 0) {
  846. devwarn(usbdev, "adding encryption key %d failed (%08X)",
  847. index+1, ret);
  848. return ret;
  849. }
  850. priv->encr_key_len[index] = key_len;
  851. priv->encr_key_wpa[index] = 0;
  852. memcpy(&priv->encr_keys[index], key, key_len);
  853. return 0;
  854. }
  855. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  856. int index, const struct sockaddr *addr,
  857. const u8 *rx_seq, int alg, int flags)
  858. {
  859. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  860. struct ndis_80211_key ndis_key;
  861. int ret;
  862. if (index < 0 || index >= 4)
  863. return -EINVAL;
  864. if (key_len > sizeof(ndis_key.material) || key_len < 0)
  865. return -EINVAL;
  866. if ((flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) && !rx_seq)
  867. return -EINVAL;
  868. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !addr)
  869. return -EINVAL;
  870. devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
  871. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  872. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  873. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  874. memset(&ndis_key, 0, sizeof(ndis_key));
  875. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  876. sizeof(ndis_key.material) + key_len);
  877. ndis_key.length = cpu_to_le32(key_len);
  878. ndis_key.index = cpu_to_le32(index) | flags;
  879. if (alg == IW_ENCODE_ALG_TKIP && key_len == 32) {
  880. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  881. * different order than NDIS spec, so swap the order here. */
  882. memcpy(ndis_key.material, key, 16);
  883. memcpy(ndis_key.material + 16, key + 24, 8);
  884. memcpy(ndis_key.material + 24, key + 16, 8);
  885. } else
  886. memcpy(ndis_key.material, key, key_len);
  887. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  888. memcpy(ndis_key.rsc, rx_seq, 6);
  889. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  890. /* pairwise key */
  891. memcpy(ndis_key.bssid, addr->sa_data, ETH_ALEN);
  892. } else {
  893. /* group key */
  894. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  895. memset(ndis_key.bssid, 0xff, ETH_ALEN);
  896. else
  897. get_bssid(usbdev, ndis_key.bssid);
  898. }
  899. ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
  900. le32_to_cpu(ndis_key.size));
  901. devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
  902. if (ret != 0)
  903. return ret;
  904. priv->encr_key_len[index] = key_len;
  905. priv->encr_key_wpa[index] = 1;
  906. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  907. priv->encr_tx_key_index = index;
  908. return 0;
  909. }
  910. /* remove_key is for both wep and wpa */
  911. static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
  912. {
  913. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  914. struct ndis_80211_remove_key remove_key;
  915. __le32 keyindex;
  916. int ret;
  917. if (priv->encr_key_len[index] == 0)
  918. return 0;
  919. priv->encr_key_len[index] = 0;
  920. priv->encr_key_wpa[index] = 0;
  921. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  922. if (priv->wpa_cipher_pair == IW_AUTH_CIPHER_TKIP ||
  923. priv->wpa_cipher_pair == IW_AUTH_CIPHER_CCMP ||
  924. priv->wpa_cipher_group == IW_AUTH_CIPHER_TKIP ||
  925. priv->wpa_cipher_group == IW_AUTH_CIPHER_CCMP) {
  926. remove_key.size = cpu_to_le32(sizeof(remove_key));
  927. remove_key.index = cpu_to_le32(index);
  928. if (bssid) {
  929. /* pairwise key */
  930. if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
  931. remove_key.index |=
  932. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  933. memcpy(remove_key.bssid, bssid,
  934. sizeof(remove_key.bssid));
  935. } else
  936. memset(remove_key.bssid, 0xff,
  937. sizeof(remove_key.bssid));
  938. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
  939. sizeof(remove_key));
  940. if (ret != 0)
  941. return ret;
  942. } else {
  943. keyindex = cpu_to_le32(index);
  944. ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
  945. sizeof(keyindex));
  946. if (ret != 0) {
  947. devwarn(usbdev,
  948. "removing encryption key %d failed (%08X)",
  949. index, ret);
  950. return ret;
  951. }
  952. }
  953. /* if it is transmit key, disable encryption */
  954. if (index == priv->encr_tx_key_index)
  955. set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
  956. return 0;
  957. }
  958. static void set_multicast_list(struct usbnet *usbdev)
  959. {
  960. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  961. struct dev_mc_list *mclist;
  962. __le32 filter;
  963. int ret, i, size;
  964. char *buf;
  965. filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  966. if (usbdev->net->flags & IFF_PROMISC) {
  967. filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
  968. RNDIS_PACKET_TYPE_ALL_LOCAL;
  969. } else if (usbdev->net->flags & IFF_ALLMULTI ||
  970. usbdev->net->mc_count > priv->multicast_size) {
  971. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  972. } else if (usbdev->net->mc_count > 0) {
  973. size = min(priv->multicast_size, usbdev->net->mc_count);
  974. buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
  975. if (!buf) {
  976. devwarn(usbdev,
  977. "couldn't alloc %d bytes of memory",
  978. size * ETH_ALEN);
  979. return;
  980. }
  981. mclist = usbdev->net->mc_list;
  982. for (i = 0; i < size && mclist; mclist = mclist->next) {
  983. if (mclist->dmi_addrlen != ETH_ALEN)
  984. continue;
  985. memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
  986. i++;
  987. }
  988. ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
  989. i * ETH_ALEN);
  990. if (ret == 0 && i > 0)
  991. filter |= RNDIS_PACKET_TYPE_MULTICAST;
  992. else
  993. filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
  994. devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
  995. i, priv->multicast_size, ret);
  996. kfree(buf);
  997. }
  998. ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
  999. sizeof(filter));
  1000. if (ret < 0) {
  1001. devwarn(usbdev, "couldn't set packet filter: %08x",
  1002. le32_to_cpu(filter));
  1003. }
  1004. devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
  1005. le32_to_cpu(filter), ret);
  1006. }
  1007. /*
  1008. * cfg80211 ops
  1009. */
  1010. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1011. struct net_device *dev,
  1012. enum nl80211_iftype type, u32 *flags,
  1013. struct vif_params *params)
  1014. {
  1015. struct usbnet *usbdev = netdev_priv(dev);
  1016. int mode;
  1017. switch (type) {
  1018. case NL80211_IFTYPE_ADHOC:
  1019. mode = NDIS_80211_INFRA_ADHOC;
  1020. break;
  1021. case NL80211_IFTYPE_STATION:
  1022. mode = NDIS_80211_INFRA_INFRA;
  1023. break;
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. return set_infra_mode(usbdev, mode);
  1028. }
  1029. #define SCAN_DELAY_JIFFIES (HZ)
  1030. static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
  1031. struct cfg80211_scan_request *request)
  1032. {
  1033. struct usbnet *usbdev = netdev_priv(dev);
  1034. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1035. int ret;
  1036. __le32 tmp;
  1037. devdbg(usbdev, "cfg80211.scan");
  1038. if (!request)
  1039. return -EINVAL;
  1040. if (priv->scan_request && priv->scan_request != request)
  1041. return -EBUSY;
  1042. priv->scan_request = request;
  1043. tmp = cpu_to_le32(1);
  1044. ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1045. sizeof(tmp));
  1046. if (ret == 0) {
  1047. /* Wait before retrieving scan results from device */
  1048. queue_delayed_work(priv->workqueue, &priv->scan_work,
  1049. SCAN_DELAY_JIFFIES);
  1050. }
  1051. return ret;
  1052. }
  1053. static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
  1054. struct ndis_80211_bssid_ex *bssid)
  1055. {
  1056. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1057. struct ieee80211_channel *channel;
  1058. s32 signal;
  1059. u64 timestamp;
  1060. u16 capability;
  1061. u16 beacon_interval;
  1062. struct ndis_80211_fixed_ies *fixed;
  1063. int ie_len, bssid_len;
  1064. u8 *ie;
  1065. /* parse bssid structure */
  1066. bssid_len = le32_to_cpu(bssid->length);
  1067. if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
  1068. sizeof(struct ndis_80211_fixed_ies))
  1069. return NULL;
  1070. fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
  1071. ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
  1072. ie_len = min(bssid_len - (int)sizeof(*bssid),
  1073. (int)le32_to_cpu(bssid->ie_length));
  1074. ie_len -= sizeof(struct ndis_80211_fixed_ies);
  1075. if (ie_len < 0)
  1076. return NULL;
  1077. /* extract data for cfg80211_inform_bss */
  1078. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1079. KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
  1080. if (!channel)
  1081. return NULL;
  1082. signal = level_to_qual(le32_to_cpu(bssid->rssi));
  1083. timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
  1084. capability = le16_to_cpu(fixed->capabilities);
  1085. beacon_interval = le16_to_cpu(fixed->beacon_interval);
  1086. return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
  1087. timestamp, capability, beacon_interval, ie, ie_len, signal,
  1088. GFP_KERNEL);
  1089. }
  1090. static int rndis_check_bssid_list(struct usbnet *usbdev)
  1091. {
  1092. void *buf = NULL;
  1093. struct ndis_80211_bssid_list_ex *bssid_list;
  1094. struct ndis_80211_bssid_ex *bssid;
  1095. int ret = -EINVAL, len, count, bssid_len;
  1096. devdbg(usbdev, "check_bssid_list");
  1097. len = CONTROL_BUFFER_SIZE;
  1098. buf = kmalloc(len, GFP_KERNEL);
  1099. if (!buf) {
  1100. ret = -ENOMEM;
  1101. goto out;
  1102. }
  1103. ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1104. if (ret != 0)
  1105. goto out;
  1106. bssid_list = buf;
  1107. bssid = bssid_list->bssid;
  1108. bssid_len = le32_to_cpu(bssid->length);
  1109. count = le32_to_cpu(bssid_list->num_items);
  1110. devdbg(usbdev, "check_bssid_list: %d BSSIDs found", count);
  1111. while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
  1112. rndis_bss_info_update(usbdev, bssid);
  1113. bssid = (void *)bssid + bssid_len;
  1114. bssid_len = le32_to_cpu(bssid->length);
  1115. count--;
  1116. }
  1117. out:
  1118. kfree(buf);
  1119. return ret;
  1120. }
  1121. static void rndis_get_scan_results(struct work_struct *work)
  1122. {
  1123. struct rndis_wlan_private *priv =
  1124. container_of(work, struct rndis_wlan_private, scan_work.work);
  1125. struct usbnet *usbdev = priv->usbdev;
  1126. int ret;
  1127. devdbg(usbdev, "get_scan_results");
  1128. ret = rndis_check_bssid_list(usbdev);
  1129. cfg80211_scan_done(priv->scan_request, ret < 0);
  1130. priv->scan_request = NULL;
  1131. }
  1132. /*
  1133. * wireless extension handlers
  1134. */
  1135. static int rndis_iw_commit(struct net_device *dev,
  1136. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1137. {
  1138. /* dummy op */
  1139. return 0;
  1140. }
  1141. static int rndis_iw_set_essid(struct net_device *dev,
  1142. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1143. {
  1144. struct ndis_80211_ssid ssid;
  1145. int length = wrqu->essid.length;
  1146. struct usbnet *usbdev = netdev_priv(dev);
  1147. devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
  1148. wrqu->essid.flags, wrqu->essid.length, essid);
  1149. if (length > NDIS_802_11_LENGTH_SSID)
  1150. length = NDIS_802_11_LENGTH_SSID;
  1151. ssid.length = cpu_to_le32(length);
  1152. if (length > 0)
  1153. memcpy(ssid.essid, essid, length);
  1154. else
  1155. memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
  1156. set_assoc_params(usbdev);
  1157. if (!wrqu->essid.flags || length == 0)
  1158. return disassociate(usbdev, 1);
  1159. else
  1160. return set_essid(usbdev, &ssid);
  1161. }
  1162. static int rndis_iw_get_essid(struct net_device *dev,
  1163. struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
  1164. {
  1165. struct ndis_80211_ssid ssid;
  1166. struct usbnet *usbdev = netdev_priv(dev);
  1167. int ret;
  1168. ret = get_essid(usbdev, &ssid);
  1169. if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
  1170. wrqu->essid.flags = 1;
  1171. wrqu->essid.length = le32_to_cpu(ssid.length);
  1172. memcpy(essid, ssid.essid, wrqu->essid.length);
  1173. essid[wrqu->essid.length] = 0;
  1174. } else {
  1175. memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
  1176. wrqu->essid.flags = 0;
  1177. wrqu->essid.length = 0;
  1178. }
  1179. devdbg(usbdev, "SIOCGIWESSID: %s", essid);
  1180. return ret;
  1181. }
  1182. static int rndis_iw_get_bssid(struct net_device *dev,
  1183. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1184. {
  1185. struct usbnet *usbdev = netdev_priv(dev);
  1186. unsigned char bssid[ETH_ALEN];
  1187. int ret;
  1188. ret = get_bssid(usbdev, bssid);
  1189. if (ret == 0)
  1190. devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
  1191. else
  1192. devdbg(usbdev, "SIOCGIWAP: <not associated>");
  1193. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1194. memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
  1195. return ret;
  1196. }
  1197. static int rndis_iw_set_bssid(struct net_device *dev,
  1198. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1199. {
  1200. struct usbnet *usbdev = netdev_priv(dev);
  1201. u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
  1202. int ret;
  1203. devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
  1204. ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
  1205. /* user apps may set ap's mac address, which is not required;
  1206. * they may fail to work if this function fails, so return
  1207. * success */
  1208. if (ret)
  1209. devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
  1210. return 0;
  1211. }
  1212. static int rndis_iw_set_auth(struct net_device *dev,
  1213. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1214. {
  1215. struct iw_param *p = &wrqu->param;
  1216. struct usbnet *usbdev = netdev_priv(dev);
  1217. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1218. int ret = -ENOTSUPP;
  1219. switch (p->flags & IW_AUTH_INDEX) {
  1220. case IW_AUTH_WPA_VERSION:
  1221. devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
  1222. priv->wpa_version = p->value;
  1223. ret = 0;
  1224. break;
  1225. case IW_AUTH_CIPHER_PAIRWISE:
  1226. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
  1227. priv->wpa_cipher_pair = p->value;
  1228. ret = 0;
  1229. break;
  1230. case IW_AUTH_CIPHER_GROUP:
  1231. devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
  1232. priv->wpa_cipher_group = p->value;
  1233. ret = 0;
  1234. break;
  1235. case IW_AUTH_KEY_MGMT:
  1236. devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
  1237. priv->wpa_keymgmt = p->value;
  1238. ret = 0;
  1239. break;
  1240. case IW_AUTH_TKIP_COUNTERMEASURES:
  1241. devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
  1242. p->value);
  1243. ret = 0;
  1244. break;
  1245. case IW_AUTH_DROP_UNENCRYPTED:
  1246. devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
  1247. ret = 0;
  1248. break;
  1249. case IW_AUTH_80211_AUTH_ALG:
  1250. devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
  1251. priv->wpa_authalg = p->value;
  1252. ret = 0;
  1253. break;
  1254. case IW_AUTH_WPA_ENABLED:
  1255. devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
  1256. if (wrqu->param.value)
  1257. deauthenticate(usbdev);
  1258. ret = 0;
  1259. break;
  1260. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  1261. devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
  1262. p->value);
  1263. ret = 0;
  1264. break;
  1265. case IW_AUTH_ROAMING_CONTROL:
  1266. devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
  1267. ret = 0;
  1268. break;
  1269. case IW_AUTH_PRIVACY_INVOKED:
  1270. devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
  1271. wrqu->param.flags & IW_AUTH_INDEX);
  1272. return -EOPNOTSUPP;
  1273. default:
  1274. devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
  1275. p->flags & IW_AUTH_INDEX, p->value);
  1276. }
  1277. return ret;
  1278. }
  1279. static int rndis_iw_get_auth(struct net_device *dev,
  1280. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1281. {
  1282. struct iw_param *p = &wrqu->param;
  1283. struct usbnet *usbdev = netdev_priv(dev);
  1284. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1285. switch (p->flags & IW_AUTH_INDEX) {
  1286. case IW_AUTH_WPA_VERSION:
  1287. p->value = priv->wpa_version;
  1288. break;
  1289. case IW_AUTH_CIPHER_PAIRWISE:
  1290. p->value = priv->wpa_cipher_pair;
  1291. break;
  1292. case IW_AUTH_CIPHER_GROUP:
  1293. p->value = priv->wpa_cipher_group;
  1294. break;
  1295. case IW_AUTH_KEY_MGMT:
  1296. p->value = priv->wpa_keymgmt;
  1297. break;
  1298. case IW_AUTH_80211_AUTH_ALG:
  1299. p->value = priv->wpa_authalg;
  1300. break;
  1301. default:
  1302. devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
  1303. wrqu->param.flags & IW_AUTH_INDEX);
  1304. return -EOPNOTSUPP;
  1305. }
  1306. return 0;
  1307. }
  1308. static int rndis_iw_set_encode(struct net_device *dev,
  1309. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1310. {
  1311. struct usbnet *usbdev = netdev_priv(dev);
  1312. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1313. int ret, index, key_len;
  1314. u8 *key;
  1315. index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
  1316. /* iwconfig gives index as 1 - N */
  1317. if (index > 0)
  1318. index--;
  1319. else
  1320. index = priv->encr_tx_key_index;
  1321. if (index < 0 || index >= 4) {
  1322. devwarn(usbdev, "encryption index out of range (%u)", index);
  1323. return -EINVAL;
  1324. }
  1325. /* remove key if disabled */
  1326. if (wrqu->data.flags & IW_ENCODE_DISABLED) {
  1327. if (remove_key(usbdev, index, NULL))
  1328. return -EINVAL;
  1329. else
  1330. return 0;
  1331. }
  1332. /* global encryption state (for all keys) */
  1333. if (wrqu->data.flags & IW_ENCODE_OPEN)
  1334. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1335. IW_AUTH_ALG_OPEN_SYSTEM);
  1336. else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
  1337. ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
  1338. IW_AUTH_ALG_SHARED_KEY);
  1339. if (ret != 0)
  1340. return ret;
  1341. if (wrqu->data.length > 0) {
  1342. key_len = wrqu->data.length;
  1343. key = extra;
  1344. } else {
  1345. /* must be set as tx key */
  1346. if (priv->encr_key_len[index] == 0)
  1347. return -EINVAL;
  1348. key_len = priv->encr_key_len[index];
  1349. key = priv->encr_keys[index];
  1350. priv->encr_tx_key_index = index;
  1351. }
  1352. if (add_wep_key(usbdev, key, key_len, index) != 0)
  1353. return -EINVAL;
  1354. if (index == priv->encr_tx_key_index)
  1355. /* ndis drivers want essid to be set after setting encr */
  1356. set_essid(usbdev, &priv->essid);
  1357. return 0;
  1358. }
  1359. static int rndis_iw_set_encode_ext(struct net_device *dev,
  1360. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1361. {
  1362. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1363. struct usbnet *usbdev = netdev_priv(dev);
  1364. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1365. int keyidx, flags;
  1366. keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
  1367. /* iwconfig gives index as 1 - N */
  1368. if (keyidx)
  1369. keyidx--;
  1370. else
  1371. keyidx = priv->encr_tx_key_index;
  1372. if (keyidx < 0 || keyidx >= 4)
  1373. return -EINVAL;
  1374. if (ext->alg == WPA_ALG_WEP) {
  1375. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1376. priv->encr_tx_key_index = keyidx;
  1377. return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
  1378. }
  1379. if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
  1380. ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
  1381. return remove_key(usbdev, keyidx, NULL);
  1382. flags = 0;
  1383. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
  1384. flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
  1385. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
  1386. flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1387. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
  1388. flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
  1389. return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx, &ext->addr,
  1390. ext->rx_seq, ext->alg, flags);
  1391. }
  1392. static int rndis_iw_set_genie(struct net_device *dev,
  1393. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1394. {
  1395. struct usbnet *usbdev = netdev_priv(dev);
  1396. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1397. int ret = 0;
  1398. #ifdef DEBUG
  1399. int j;
  1400. u8 *gie = extra;
  1401. for (j = 0; j < wrqu->data.length; j += 8)
  1402. devdbg(usbdev,
  1403. "SIOCSIWGENIE %04x - "
  1404. "%02x %02x %02x %02x %02x %02x %02x %02x", j,
  1405. gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
  1406. gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
  1407. #endif
  1408. /* clear existing IEs */
  1409. if (priv->wpa_ie_len) {
  1410. kfree(priv->wpa_ie);
  1411. priv->wpa_ie_len = 0;
  1412. }
  1413. /* set new IEs */
  1414. priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
  1415. if (priv->wpa_ie) {
  1416. priv->wpa_ie_len = wrqu->data.length;
  1417. memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
  1418. } else
  1419. ret = -ENOMEM;
  1420. return ret;
  1421. }
  1422. static int rndis_iw_get_genie(struct net_device *dev,
  1423. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1424. {
  1425. struct usbnet *usbdev = netdev_priv(dev);
  1426. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1427. devdbg(usbdev, "SIOCGIWGENIE");
  1428. if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
  1429. wrqu->data.length = 0;
  1430. return 0;
  1431. }
  1432. if (wrqu->data.length < priv->wpa_ie_len)
  1433. return -E2BIG;
  1434. wrqu->data.length = priv->wpa_ie_len;
  1435. memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
  1436. return 0;
  1437. }
  1438. static int rndis_iw_set_rts(struct net_device *dev,
  1439. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1440. {
  1441. struct usbnet *usbdev = netdev_priv(dev);
  1442. __le32 tmp;
  1443. devdbg(usbdev, "SIOCSIWRTS");
  1444. tmp = cpu_to_le32(wrqu->rts.value);
  1445. return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
  1446. sizeof(tmp));
  1447. }
  1448. static int rndis_iw_get_rts(struct net_device *dev,
  1449. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1450. {
  1451. struct usbnet *usbdev = netdev_priv(dev);
  1452. __le32 tmp;
  1453. int len, ret;
  1454. len = sizeof(tmp);
  1455. ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
  1456. if (ret == 0) {
  1457. wrqu->rts.value = le32_to_cpu(tmp);
  1458. wrqu->rts.flags = 1;
  1459. wrqu->rts.disabled = 0;
  1460. }
  1461. devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
  1462. return ret;
  1463. }
  1464. static int rndis_iw_set_frag(struct net_device *dev,
  1465. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1466. {
  1467. struct usbnet *usbdev = netdev_priv(dev);
  1468. __le32 tmp;
  1469. devdbg(usbdev, "SIOCSIWFRAG");
  1470. tmp = cpu_to_le32(wrqu->frag.value);
  1471. return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  1472. sizeof(tmp));
  1473. }
  1474. static int rndis_iw_get_frag(struct net_device *dev,
  1475. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1476. {
  1477. struct usbnet *usbdev = netdev_priv(dev);
  1478. __le32 tmp;
  1479. int len, ret;
  1480. len = sizeof(tmp);
  1481. ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
  1482. &len);
  1483. if (ret == 0) {
  1484. wrqu->frag.value = le32_to_cpu(tmp);
  1485. wrqu->frag.flags = 1;
  1486. wrqu->frag.disabled = 0;
  1487. }
  1488. devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
  1489. return ret;
  1490. }
  1491. static int rndis_iw_set_freq(struct net_device *dev,
  1492. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1493. {
  1494. struct usbnet *usbdev = netdev_priv(dev);
  1495. struct ndis_80211_conf config;
  1496. unsigned int dsconfig;
  1497. int len, ret;
  1498. /* this OID is valid only when not associated */
  1499. if (is_associated(usbdev))
  1500. return 0;
  1501. dsconfig = 0;
  1502. if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
  1503. return -EINVAL;
  1504. len = sizeof(config);
  1505. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1506. if (ret != 0) {
  1507. devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
  1508. return 0;
  1509. }
  1510. config.ds_config = cpu_to_le32(dsconfig);
  1511. devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
  1512. return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
  1513. sizeof(config));
  1514. }
  1515. static int rndis_iw_get_freq(struct net_device *dev,
  1516. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1517. {
  1518. struct usbnet *usbdev = netdev_priv(dev);
  1519. struct ndis_80211_conf config;
  1520. int len, ret;
  1521. len = sizeof(config);
  1522. ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
  1523. if (ret == 0)
  1524. dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
  1525. devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
  1526. return ret;
  1527. }
  1528. static int rndis_iw_get_txpower(struct net_device *dev,
  1529. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1530. {
  1531. struct usbnet *usbdev = netdev_priv(dev);
  1532. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1533. __le32 tx_power;
  1534. if (priv->radio_on) {
  1535. /* fake since changing tx_power (by userlevel) not supported */
  1536. tx_power = cpu_to_le32(get_bcm4320_power(priv));
  1537. wrqu->txpower.flags = IW_TXPOW_MWATT;
  1538. wrqu->txpower.value = le32_to_cpu(tx_power);
  1539. wrqu->txpower.disabled = 0;
  1540. } else {
  1541. wrqu->txpower.flags = IW_TXPOW_MWATT;
  1542. wrqu->txpower.value = 0;
  1543. wrqu->txpower.disabled = 1;
  1544. }
  1545. devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
  1546. return 0;
  1547. }
  1548. static int rndis_iw_set_txpower(struct net_device *dev,
  1549. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1550. {
  1551. struct usbnet *usbdev = netdev_priv(dev);
  1552. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1553. __le32 tx_power = 0;
  1554. if (!wrqu->txpower.disabled) {
  1555. if (wrqu->txpower.flags == IW_TXPOW_MWATT)
  1556. tx_power = cpu_to_le32(wrqu->txpower.value);
  1557. else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
  1558. if (wrqu->txpower.value > 20)
  1559. tx_power = cpu_to_le32(128);
  1560. else if (wrqu->txpower.value < -43)
  1561. tx_power = cpu_to_le32(127);
  1562. else {
  1563. signed char tmp;
  1564. tmp = wrqu->txpower.value;
  1565. tmp = -12 - tmp;
  1566. tmp <<= 2;
  1567. tx_power = cpu_to_le32((unsigned char)tmp);
  1568. }
  1569. }
  1570. }
  1571. devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
  1572. if (le32_to_cpu(tx_power) != 0) {
  1573. /* txpower unsupported, just turn radio on */
  1574. if (!priv->radio_on)
  1575. return disassociate(usbdev, 1);
  1576. return 0; /* all ready on */
  1577. }
  1578. /* tx_power == 0, turn off radio */
  1579. return disassociate(usbdev, 0);
  1580. }
  1581. static int rndis_iw_get_rate(struct net_device *dev,
  1582. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1583. {
  1584. struct usbnet *usbdev = netdev_priv(dev);
  1585. __le32 tmp;
  1586. int ret, len;
  1587. len = sizeof(tmp);
  1588. ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
  1589. if (ret == 0) {
  1590. wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
  1591. wrqu->bitrate.disabled = 0;
  1592. wrqu->bitrate.flags = 1;
  1593. }
  1594. return ret;
  1595. }
  1596. static int rndis_iw_set_mlme(struct net_device *dev,
  1597. struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
  1598. {
  1599. struct usbnet *usbdev = netdev_priv(dev);
  1600. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1601. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  1602. unsigned char bssid[ETH_ALEN];
  1603. get_bssid(usbdev, bssid);
  1604. if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
  1605. return -EINVAL;
  1606. switch (mlme->cmd) {
  1607. case IW_MLME_DEAUTH:
  1608. return deauthenticate(usbdev);
  1609. case IW_MLME_DISASSOC:
  1610. return disassociate(usbdev, priv->radio_on);
  1611. default:
  1612. return -EOPNOTSUPP;
  1613. }
  1614. return 0;
  1615. }
  1616. static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
  1617. {
  1618. struct usbnet *usbdev = netdev_priv(dev);
  1619. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1620. unsigned long flags;
  1621. spin_lock_irqsave(&priv->stats_lock, flags);
  1622. memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
  1623. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1624. return &priv->iwstats;
  1625. }
  1626. #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
  1627. static const iw_handler rndis_iw_handler[] =
  1628. {
  1629. IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
  1630. IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
  1631. IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
  1632. IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
  1633. IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
  1634. IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
  1635. IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
  1636. IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
  1637. IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
  1638. IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
  1639. IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
  1640. IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
  1641. IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
  1642. IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
  1643. IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
  1644. IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
  1645. IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
  1646. IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
  1647. IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
  1648. IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
  1649. IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
  1650. IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
  1651. IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
  1652. IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
  1653. IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
  1654. IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
  1655. IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
  1656. };
  1657. static const iw_handler rndis_wlan_private_handler[] = {
  1658. };
  1659. static const struct iw_priv_args rndis_wlan_private_args[] = {
  1660. };
  1661. static const struct iw_handler_def rndis_iw_handlers = {
  1662. .num_standard = ARRAY_SIZE(rndis_iw_handler),
  1663. .num_private = ARRAY_SIZE(rndis_wlan_private_handler),
  1664. .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
  1665. .standard = (iw_handler *)rndis_iw_handler,
  1666. .private = (iw_handler *)rndis_wlan_private_handler,
  1667. .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
  1668. .get_wireless_stats = rndis_get_wireless_stats,
  1669. };
  1670. static void rndis_wlan_worker(struct work_struct *work)
  1671. {
  1672. struct rndis_wlan_private *priv =
  1673. container_of(work, struct rndis_wlan_private, work);
  1674. struct usbnet *usbdev = priv->usbdev;
  1675. union iwreq_data evt;
  1676. unsigned char bssid[ETH_ALEN];
  1677. struct ndis_80211_assoc_info *info;
  1678. int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
  1679. int ret, offset;
  1680. if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
  1681. netif_carrier_on(usbdev->net);
  1682. info = kzalloc(assoc_size, GFP_KERNEL);
  1683. if (!info)
  1684. goto get_bssid;
  1685. /* Get association info IEs from device and send them back to
  1686. * userspace. */
  1687. ret = get_association_info(usbdev, info, assoc_size);
  1688. if (!ret) {
  1689. evt.data.length = le32_to_cpu(info->req_ie_length);
  1690. if (evt.data.length > 0) {
  1691. offset = le32_to_cpu(info->offset_req_ies);
  1692. wireless_send_event(usbdev->net,
  1693. IWEVASSOCREQIE, &evt,
  1694. (char *)info + offset);
  1695. }
  1696. evt.data.length = le32_to_cpu(info->resp_ie_length);
  1697. if (evt.data.length > 0) {
  1698. offset = le32_to_cpu(info->offset_resp_ies);
  1699. wireless_send_event(usbdev->net,
  1700. IWEVASSOCRESPIE, &evt,
  1701. (char *)info + offset);
  1702. }
  1703. }
  1704. kfree(info);
  1705. get_bssid:
  1706. ret = get_bssid(usbdev, bssid);
  1707. if (!ret) {
  1708. evt.data.flags = 0;
  1709. evt.data.length = 0;
  1710. memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
  1711. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1712. }
  1713. }
  1714. if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
  1715. netif_carrier_off(usbdev->net);
  1716. evt.data.flags = 0;
  1717. evt.data.length = 0;
  1718. memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
  1719. wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
  1720. }
  1721. if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1722. set_multicast_list(usbdev);
  1723. }
  1724. static void rndis_wlan_set_multicast_list(struct net_device *dev)
  1725. {
  1726. struct usbnet *usbdev = netdev_priv(dev);
  1727. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1728. if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
  1729. return;
  1730. set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
  1731. queue_work(priv->workqueue, &priv->work);
  1732. }
  1733. static void rndis_wlan_link_change(struct usbnet *usbdev, int state)
  1734. {
  1735. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1736. /* queue work to avoid recursive calls into rndis_command */
  1737. set_bit(state ? WORK_LINK_UP : WORK_LINK_DOWN, &priv->work_pending);
  1738. queue_work(priv->workqueue, &priv->work);
  1739. }
  1740. static int rndis_wlan_get_caps(struct usbnet *usbdev)
  1741. {
  1742. struct {
  1743. __le32 num_items;
  1744. __le32 items[8];
  1745. } networks_supported;
  1746. int len, retval, i, n;
  1747. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1748. /* determine supported modes */
  1749. len = sizeof(networks_supported);
  1750. retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
  1751. &networks_supported, &len);
  1752. if (retval >= 0) {
  1753. n = le32_to_cpu(networks_supported.num_items);
  1754. if (n > 8)
  1755. n = 8;
  1756. for (i = 0; i < n; i++) {
  1757. switch (le32_to_cpu(networks_supported.items[i])) {
  1758. case NDIS_80211_TYPE_FREQ_HOP:
  1759. case NDIS_80211_TYPE_DIRECT_SEQ:
  1760. priv->caps |= CAP_MODE_80211B;
  1761. break;
  1762. case NDIS_80211_TYPE_OFDM_A:
  1763. priv->caps |= CAP_MODE_80211A;
  1764. break;
  1765. case NDIS_80211_TYPE_OFDM_G:
  1766. priv->caps |= CAP_MODE_80211G;
  1767. break;
  1768. }
  1769. }
  1770. }
  1771. return retval;
  1772. }
  1773. #define STATS_UPDATE_JIFFIES (HZ)
  1774. static void rndis_update_wireless_stats(struct work_struct *work)
  1775. {
  1776. struct rndis_wlan_private *priv =
  1777. container_of(work, struct rndis_wlan_private, stats_work.work);
  1778. struct usbnet *usbdev = priv->usbdev;
  1779. struct iw_statistics iwstats;
  1780. __le32 rssi, tmp;
  1781. int len, ret, j;
  1782. unsigned long flags;
  1783. int update_jiffies = STATS_UPDATE_JIFFIES;
  1784. void *buf;
  1785. spin_lock_irqsave(&priv->stats_lock, flags);
  1786. memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
  1787. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1788. /* only update stats when connected */
  1789. if (!is_associated(usbdev)) {
  1790. iwstats.qual.qual = 0;
  1791. iwstats.qual.level = 0;
  1792. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  1793. | IW_QUAL_LEVEL_UPDATED
  1794. | IW_QUAL_NOISE_INVALID
  1795. | IW_QUAL_QUAL_INVALID
  1796. | IW_QUAL_LEVEL_INVALID;
  1797. goto end;
  1798. }
  1799. len = sizeof(rssi);
  1800. ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
  1801. devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
  1802. le32_to_cpu(rssi));
  1803. if (ret == 0) {
  1804. memset(&iwstats.qual, 0, sizeof(iwstats.qual));
  1805. iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
  1806. iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
  1807. iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  1808. | IW_QUAL_LEVEL_UPDATED
  1809. | IW_QUAL_NOISE_INVALID;
  1810. }
  1811. memset(&iwstats.discard, 0, sizeof(iwstats.discard));
  1812. len = sizeof(tmp);
  1813. ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
  1814. if (ret == 0)
  1815. iwstats.discard.misc += le32_to_cpu(tmp);
  1816. len = sizeof(tmp);
  1817. ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
  1818. if (ret == 0)
  1819. iwstats.discard.misc += le32_to_cpu(tmp);
  1820. len = sizeof(tmp);
  1821. ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
  1822. if (ret == 0)
  1823. iwstats.discard.misc += le32_to_cpu(tmp);
  1824. /* Workaround transfer stalls on poor quality links.
  1825. * TODO: find right way to fix these stalls (as stalls do not happen
  1826. * with ndiswrapper/windows driver). */
  1827. if (iwstats.qual.qual <= 25) {
  1828. /* Decrease stats worker interval to catch stalls.
  1829. * faster. Faster than 400-500ms causes packet loss,
  1830. * Slower doesn't catch stalls fast enough.
  1831. */
  1832. j = msecs_to_jiffies(priv->param_workaround_interval);
  1833. if (j > STATS_UPDATE_JIFFIES)
  1834. j = STATS_UPDATE_JIFFIES;
  1835. else if (j <= 0)
  1836. j = 1;
  1837. update_jiffies = j;
  1838. /* Send scan OID. Use of both OIDs is required to get device
  1839. * working.
  1840. */
  1841. tmp = cpu_to_le32(1);
  1842. rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
  1843. sizeof(tmp));
  1844. len = CONTROL_BUFFER_SIZE;
  1845. buf = kmalloc(len, GFP_KERNEL);
  1846. if (!buf)
  1847. goto end;
  1848. rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
  1849. kfree(buf);
  1850. }
  1851. end:
  1852. spin_lock_irqsave(&priv->stats_lock, flags);
  1853. memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
  1854. spin_unlock_irqrestore(&priv->stats_lock, flags);
  1855. if (update_jiffies >= HZ)
  1856. update_jiffies = round_jiffies_relative(update_jiffies);
  1857. else {
  1858. j = round_jiffies_relative(update_jiffies);
  1859. if (abs(j - update_jiffies) <= 10)
  1860. update_jiffies = j;
  1861. }
  1862. queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
  1863. }
  1864. static int bcm4320a_early_init(struct usbnet *usbdev)
  1865. {
  1866. /* bcm4320a doesn't handle configuration parameters well. Try
  1867. * set any and you get partially zeroed mac and broken device.
  1868. */
  1869. return 0;
  1870. }
  1871. static int bcm4320b_early_init(struct usbnet *usbdev)
  1872. {
  1873. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1874. char buf[8];
  1875. /* Early initialization settings, setting these won't have effect
  1876. * if called after generic_rndis_bind().
  1877. */
  1878. priv->param_country[0] = modparam_country[0];
  1879. priv->param_country[1] = modparam_country[1];
  1880. priv->param_country[2] = 0;
  1881. priv->param_frameburst = modparam_frameburst;
  1882. priv->param_afterburner = modparam_afterburner;
  1883. priv->param_power_save = modparam_power_save;
  1884. priv->param_power_output = modparam_power_output;
  1885. priv->param_roamtrigger = modparam_roamtrigger;
  1886. priv->param_roamdelta = modparam_roamdelta;
  1887. priv->param_country[0] = toupper(priv->param_country[0]);
  1888. priv->param_country[1] = toupper(priv->param_country[1]);
  1889. /* doesn't support EU as country code, use FI instead */
  1890. if (!strcmp(priv->param_country, "EU"))
  1891. strcpy(priv->param_country, "FI");
  1892. if (priv->param_power_save < 0)
  1893. priv->param_power_save = 0;
  1894. else if (priv->param_power_save > 2)
  1895. priv->param_power_save = 2;
  1896. if (priv->param_power_output < 0)
  1897. priv->param_power_output = 0;
  1898. else if (priv->param_power_output > 3)
  1899. priv->param_power_output = 3;
  1900. if (priv->param_roamtrigger < -80)
  1901. priv->param_roamtrigger = -80;
  1902. else if (priv->param_roamtrigger > -60)
  1903. priv->param_roamtrigger = -60;
  1904. if (priv->param_roamdelta < 0)
  1905. priv->param_roamdelta = 0;
  1906. else if (priv->param_roamdelta > 2)
  1907. priv->param_roamdelta = 2;
  1908. if (modparam_workaround_interval < 0)
  1909. priv->param_workaround_interval = 500;
  1910. else
  1911. priv->param_workaround_interval = modparam_workaround_interval;
  1912. rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
  1913. rndis_set_config_parameter_str(usbdev, "FrameBursting",
  1914. priv->param_frameburst ? "1" : "0");
  1915. rndis_set_config_parameter_str(usbdev, "Afterburner",
  1916. priv->param_afterburner ? "1" : "0");
  1917. sprintf(buf, "%d", priv->param_power_save);
  1918. rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
  1919. sprintf(buf, "%d", priv->param_power_output);
  1920. rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
  1921. sprintf(buf, "%d", priv->param_roamtrigger);
  1922. rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
  1923. sprintf(buf, "%d", priv->param_roamdelta);
  1924. rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
  1925. return 0;
  1926. }
  1927. /* same as rndis_netdev_ops but with local multicast handler */
  1928. static const struct net_device_ops rndis_wlan_netdev_ops = {
  1929. .ndo_open = usbnet_open,
  1930. .ndo_stop = usbnet_stop,
  1931. .ndo_start_xmit = usbnet_start_xmit,
  1932. .ndo_tx_timeout = usbnet_tx_timeout,
  1933. .ndo_set_mac_address = eth_mac_addr,
  1934. .ndo_validate_addr = eth_validate_addr,
  1935. .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
  1936. };
  1937. static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
  1938. {
  1939. struct wiphy *wiphy;
  1940. struct rndis_wlan_private *priv;
  1941. int retval, len;
  1942. __le32 tmp;
  1943. /* allocate wiphy and rndis private data
  1944. * NOTE: We only support a single virtual interface, so wiphy
  1945. * and wireless_dev are somewhat synonymous for this device.
  1946. */
  1947. wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
  1948. if (!wiphy)
  1949. return -ENOMEM;
  1950. priv = wiphy_priv(wiphy);
  1951. usbdev->net->ieee80211_ptr = &priv->wdev;
  1952. priv->wdev.wiphy = wiphy;
  1953. priv->wdev.iftype = NL80211_IFTYPE_STATION;
  1954. /* These have to be initialized before calling generic_rndis_bind().
  1955. * Otherwise we'll be in big trouble in rndis_wlan_early_init().
  1956. */
  1957. usbdev->driver_priv = priv;
  1958. usbdev->net->wireless_handlers = &rndis_iw_handlers;
  1959. priv->usbdev = usbdev;
  1960. mutex_init(&priv->command_lock);
  1961. spin_lock_init(&priv->stats_lock);
  1962. /* because rndis_command() sleeps we need to use workqueue */
  1963. priv->workqueue = create_singlethread_workqueue("rndis_wlan");
  1964. INIT_WORK(&priv->work, rndis_wlan_worker);
  1965. INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
  1966. INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
  1967. /* try bind rndis_host */
  1968. retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
  1969. if (retval < 0)
  1970. goto fail;
  1971. /* generic_rndis_bind set packet filter to multicast_all+
  1972. * promisc mode which doesn't work well for our devices (device
  1973. * picks up rssi to closest station instead of to access point).
  1974. *
  1975. * rndis_host wants to avoid all OID as much as possible
  1976. * so do promisc/multicast handling in rndis_wlan.
  1977. */
  1978. usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
  1979. tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
  1980. retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
  1981. sizeof(tmp));
  1982. len = sizeof(tmp);
  1983. retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
  1984. &len);
  1985. priv->multicast_size = le32_to_cpu(tmp);
  1986. if (retval < 0 || priv->multicast_size < 0)
  1987. priv->multicast_size = 0;
  1988. if (priv->multicast_size > 0)
  1989. usbdev->net->flags |= IFF_MULTICAST;
  1990. else
  1991. usbdev->net->flags &= ~IFF_MULTICAST;
  1992. priv->iwstats.qual.qual = 0;
  1993. priv->iwstats.qual.level = 0;
  1994. priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
  1995. | IW_QUAL_LEVEL_UPDATED
  1996. | IW_QUAL_NOISE_INVALID
  1997. | IW_QUAL_QUAL_INVALID
  1998. | IW_QUAL_LEVEL_INVALID;
  1999. /* fill-out wiphy structure and register w/ cfg80211 */
  2000. memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
  2001. wiphy->privid = rndis_wiphy_privid;
  2002. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
  2003. | BIT(NL80211_IFTYPE_ADHOC);
  2004. wiphy->max_scan_ssids = 1;
  2005. /* TODO: fill-out band information based on priv->caps */
  2006. rndis_wlan_get_caps(usbdev);
  2007. memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
  2008. memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
  2009. priv->band.channels = priv->channels;
  2010. priv->band.n_channels = ARRAY_SIZE(rndis_channels);
  2011. priv->band.bitrates = priv->rates;
  2012. priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
  2013. wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  2014. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  2015. set_wiphy_dev(wiphy, &usbdev->udev->dev);
  2016. if (wiphy_register(wiphy)) {
  2017. retval = -ENODEV;
  2018. goto fail;
  2019. }
  2020. set_default_iw_params(usbdev);
  2021. /* turn radio on */
  2022. priv->radio_on = 1;
  2023. disassociate(usbdev, 1);
  2024. netif_carrier_off(usbdev->net);
  2025. queue_delayed_work(priv->workqueue, &priv->stats_work,
  2026. round_jiffies_relative(STATS_UPDATE_JIFFIES));
  2027. return 0;
  2028. fail:
  2029. cancel_delayed_work_sync(&priv->stats_work);
  2030. cancel_delayed_work_sync(&priv->scan_work);
  2031. cancel_work_sync(&priv->work);
  2032. flush_workqueue(priv->workqueue);
  2033. destroy_workqueue(priv->workqueue);
  2034. wiphy_free(wiphy);
  2035. return retval;
  2036. }
  2037. static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
  2038. {
  2039. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  2040. /* turn radio off */
  2041. disassociate(usbdev, 0);
  2042. cancel_delayed_work_sync(&priv->stats_work);
  2043. cancel_delayed_work_sync(&priv->scan_work);
  2044. cancel_work_sync(&priv->work);
  2045. flush_workqueue(priv->workqueue);
  2046. destroy_workqueue(priv->workqueue);
  2047. if (priv && priv->wpa_ie_len)
  2048. kfree(priv->wpa_ie);
  2049. rndis_unbind(usbdev, intf);
  2050. wiphy_unregister(priv->wdev.wiphy);
  2051. wiphy_free(priv->wdev.wiphy);
  2052. }
  2053. static int rndis_wlan_reset(struct usbnet *usbdev)
  2054. {
  2055. return deauthenticate(usbdev);
  2056. }
  2057. static const struct driver_info bcm4320b_info = {
  2058. .description = "Wireless RNDIS device, BCM4320b based",
  2059. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  2060. .bind = rndis_wlan_bind,
  2061. .unbind = rndis_wlan_unbind,
  2062. .status = rndis_status,
  2063. .rx_fixup = rndis_rx_fixup,
  2064. .tx_fixup = rndis_tx_fixup,
  2065. .reset = rndis_wlan_reset,
  2066. .early_init = bcm4320b_early_init,
  2067. .link_change = rndis_wlan_link_change,
  2068. };
  2069. static const struct driver_info bcm4320a_info = {
  2070. .description = "Wireless RNDIS device, BCM4320a based",
  2071. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  2072. .bind = rndis_wlan_bind,
  2073. .unbind = rndis_wlan_unbind,
  2074. .status = rndis_status,
  2075. .rx_fixup = rndis_rx_fixup,
  2076. .tx_fixup = rndis_tx_fixup,
  2077. .reset = rndis_wlan_reset,
  2078. .early_init = bcm4320a_early_init,
  2079. .link_change = rndis_wlan_link_change,
  2080. };
  2081. static const struct driver_info rndis_wlan_info = {
  2082. .description = "Wireless RNDIS device",
  2083. .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  2084. .bind = rndis_wlan_bind,
  2085. .unbind = rndis_wlan_unbind,
  2086. .status = rndis_status,
  2087. .rx_fixup = rndis_rx_fixup,
  2088. .tx_fixup = rndis_tx_fixup,
  2089. .reset = rndis_wlan_reset,
  2090. .early_init = bcm4320a_early_init,
  2091. .link_change = rndis_wlan_link_change,
  2092. };
  2093. /*-------------------------------------------------------------------------*/
  2094. static const struct usb_device_id products [] = {
  2095. #define RNDIS_MASTER_INTERFACE \
  2096. .bInterfaceClass = USB_CLASS_COMM, \
  2097. .bInterfaceSubClass = 2 /* ACM */, \
  2098. .bInterfaceProtocol = 0x0ff
  2099. /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
  2100. * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
  2101. */
  2102. {
  2103. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2104. | USB_DEVICE_ID_MATCH_DEVICE,
  2105. .idVendor = 0x0411,
  2106. .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
  2107. RNDIS_MASTER_INTERFACE,
  2108. .driver_info = (unsigned long) &bcm4320b_info,
  2109. }, {
  2110. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2111. | USB_DEVICE_ID_MATCH_DEVICE,
  2112. .idVendor = 0x0baf,
  2113. .idProduct = 0x011b, /* U.S. Robotics USR5421 */
  2114. RNDIS_MASTER_INTERFACE,
  2115. .driver_info = (unsigned long) &bcm4320b_info,
  2116. }, {
  2117. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2118. | USB_DEVICE_ID_MATCH_DEVICE,
  2119. .idVendor = 0x050d,
  2120. .idProduct = 0x011b, /* Belkin F5D7051 */
  2121. RNDIS_MASTER_INTERFACE,
  2122. .driver_info = (unsigned long) &bcm4320b_info,
  2123. }, {
  2124. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2125. | USB_DEVICE_ID_MATCH_DEVICE,
  2126. .idVendor = 0x1799, /* Belkin has two vendor ids */
  2127. .idProduct = 0x011b, /* Belkin F5D7051 */
  2128. RNDIS_MASTER_INTERFACE,
  2129. .driver_info = (unsigned long) &bcm4320b_info,
  2130. }, {
  2131. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2132. | USB_DEVICE_ID_MATCH_DEVICE,
  2133. .idVendor = 0x13b1,
  2134. .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
  2135. RNDIS_MASTER_INTERFACE,
  2136. .driver_info = (unsigned long) &bcm4320b_info,
  2137. }, {
  2138. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2139. | USB_DEVICE_ID_MATCH_DEVICE,
  2140. .idVendor = 0x13b1,
  2141. .idProduct = 0x0026, /* Linksys WUSB54GSC */
  2142. RNDIS_MASTER_INTERFACE,
  2143. .driver_info = (unsigned long) &bcm4320b_info,
  2144. }, {
  2145. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2146. | USB_DEVICE_ID_MATCH_DEVICE,
  2147. .idVendor = 0x0b05,
  2148. .idProduct = 0x1717, /* Asus WL169gE */
  2149. RNDIS_MASTER_INTERFACE,
  2150. .driver_info = (unsigned long) &bcm4320b_info,
  2151. }, {
  2152. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2153. | USB_DEVICE_ID_MATCH_DEVICE,
  2154. .idVendor = 0x0a5c,
  2155. .idProduct = 0xd11b, /* Eminent EM4045 */
  2156. RNDIS_MASTER_INTERFACE,
  2157. .driver_info = (unsigned long) &bcm4320b_info,
  2158. }, {
  2159. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2160. | USB_DEVICE_ID_MATCH_DEVICE,
  2161. .idVendor = 0x1690,
  2162. .idProduct = 0x0715, /* BT Voyager 1055 */
  2163. RNDIS_MASTER_INTERFACE,
  2164. .driver_info = (unsigned long) &bcm4320b_info,
  2165. },
  2166. /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
  2167. * parameters available, hardware probably contain older firmware version with
  2168. * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
  2169. */
  2170. {
  2171. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2172. | USB_DEVICE_ID_MATCH_DEVICE,
  2173. .idVendor = 0x13b1,
  2174. .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
  2175. RNDIS_MASTER_INTERFACE,
  2176. .driver_info = (unsigned long) &bcm4320a_info,
  2177. }, {
  2178. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2179. | USB_DEVICE_ID_MATCH_DEVICE,
  2180. .idVendor = 0x0baf,
  2181. .idProduct = 0x0111, /* U.S. Robotics USR5420 */
  2182. RNDIS_MASTER_INTERFACE,
  2183. .driver_info = (unsigned long) &bcm4320a_info,
  2184. }, {
  2185. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  2186. | USB_DEVICE_ID_MATCH_DEVICE,
  2187. .idVendor = 0x0411,
  2188. .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
  2189. RNDIS_MASTER_INTERFACE,
  2190. .driver_info = (unsigned long) &bcm4320a_info,
  2191. },
  2192. /* Generic Wireless RNDIS devices that we don't have exact
  2193. * idVendor/idProduct/chip yet.
  2194. */
  2195. {
  2196. /* RNDIS is MSFT's un-official variant of CDC ACM */
  2197. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  2198. .driver_info = (unsigned long) &rndis_wlan_info,
  2199. }, {
  2200. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  2201. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  2202. .driver_info = (unsigned long) &rndis_wlan_info,
  2203. },
  2204. { }, // END
  2205. };
  2206. MODULE_DEVICE_TABLE(usb, products);
  2207. static struct usb_driver rndis_wlan_driver = {
  2208. .name = "rndis_wlan",
  2209. .id_table = products,
  2210. .probe = usbnet_probe,
  2211. .disconnect = usbnet_disconnect,
  2212. .suspend = usbnet_suspend,
  2213. .resume = usbnet_resume,
  2214. };
  2215. static int __init rndis_wlan_init(void)
  2216. {
  2217. return usb_register(&rndis_wlan_driver);
  2218. }
  2219. module_init(rndis_wlan_init);
  2220. static void __exit rndis_wlan_exit(void)
  2221. {
  2222. usb_deregister(&rndis_wlan_driver);
  2223. }
  2224. module_exit(rndis_wlan_exit);
  2225. MODULE_AUTHOR("Bjorge Dijkstra");
  2226. MODULE_AUTHOR("Jussi Kivilinna");
  2227. MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
  2228. MODULE_LICENSE("GPL");