rndis_wlan.c 84 KB

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