rndis_wlan.c 72 KB

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