rndis_wlan.c 85 KB

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