ps3_gelic_wireless.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  1. /*
  2. * PS3 gelic network driver.
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2007 Sony Corporation
  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 version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #undef DEBUG
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/in.h>
  27. #include <linux/ip.h>
  28. #include <linux/tcp.h>
  29. #include <linux/wireless.h>
  30. #include <linux/ctype.h>
  31. #include <linux/string.h>
  32. #include <net/iw_handler.h>
  33. #include <net/ieee80211.h>
  34. #include <linux/dma-mapping.h>
  35. #include <net/checksum.h>
  36. #include <asm/firmware.h>
  37. #include <asm/ps3.h>
  38. #include <asm/lv1call.h>
  39. #include "ps3_gelic_net.h"
  40. #include "ps3_gelic_wireless.h"
  41. static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan);
  42. static int gelic_wl_try_associate(struct net_device *netdev);
  43. /*
  44. * tables
  45. */
  46. /* 802.11b/g channel to freq in MHz */
  47. static const int channel_freq[] = {
  48. 2412, 2417, 2422, 2427, 2432,
  49. 2437, 2442, 2447, 2452, 2457,
  50. 2462, 2467, 2472, 2484
  51. };
  52. #define NUM_CHANNELS ARRAY_SIZE(channel_freq)
  53. /* in bps */
  54. static const int bitrate_list[] = {
  55. 1000000,
  56. 2000000,
  57. 5500000,
  58. 11000000,
  59. 6000000,
  60. 9000000,
  61. 12000000,
  62. 18000000,
  63. 24000000,
  64. 36000000,
  65. 48000000,
  66. 54000000
  67. };
  68. #define NUM_BITRATES ARRAY_SIZE(bitrate_list)
  69. /*
  70. * wpa2 support requires the hypervisor version 2.0 or later
  71. */
  72. static inline int wpa2_capable(void)
  73. {
  74. return (0 <= ps3_compare_firmware_version(2, 0, 0));
  75. }
  76. static inline int precise_ie(void)
  77. {
  78. return (0 <= ps3_compare_firmware_version(2, 2, 0));
  79. }
  80. /*
  81. * post_eurus_cmd helpers
  82. */
  83. struct eurus_cmd_arg_info {
  84. int pre_arg; /* command requres arg1, arg2 at POST COMMAND */
  85. int post_arg; /* command requires arg1, arg2 at GET_RESULT */
  86. };
  87. static const struct eurus_cmd_arg_info cmd_info[GELIC_EURUS_CMD_MAX_INDEX] = {
  88. [GELIC_EURUS_CMD_SET_COMMON_CFG] = { .pre_arg = 1},
  89. [GELIC_EURUS_CMD_SET_WEP_CFG] = { .pre_arg = 1},
  90. [GELIC_EURUS_CMD_SET_WPA_CFG] = { .pre_arg = 1},
  91. [GELIC_EURUS_CMD_GET_COMMON_CFG] = { .post_arg = 1},
  92. [GELIC_EURUS_CMD_GET_WEP_CFG] = { .post_arg = 1},
  93. [GELIC_EURUS_CMD_GET_WPA_CFG] = { .post_arg = 1},
  94. [GELIC_EURUS_CMD_GET_RSSI_CFG] = { .post_arg = 1},
  95. [GELIC_EURUS_CMD_GET_SCAN] = { .post_arg = 1},
  96. };
  97. #ifdef DEBUG
  98. static const char *cmdstr(enum gelic_eurus_command ix)
  99. {
  100. switch (ix) {
  101. case GELIC_EURUS_CMD_ASSOC:
  102. return "ASSOC";
  103. case GELIC_EURUS_CMD_DISASSOC:
  104. return "DISASSOC";
  105. case GELIC_EURUS_CMD_START_SCAN:
  106. return "SCAN";
  107. case GELIC_EURUS_CMD_GET_SCAN:
  108. return "GET SCAN";
  109. case GELIC_EURUS_CMD_SET_COMMON_CFG:
  110. return "SET_COMMON_CFG";
  111. case GELIC_EURUS_CMD_GET_COMMON_CFG:
  112. return "GET_COMMON_CFG";
  113. case GELIC_EURUS_CMD_SET_WEP_CFG:
  114. return "SET_WEP_CFG";
  115. case GELIC_EURUS_CMD_GET_WEP_CFG:
  116. return "GET_WEP_CFG";
  117. case GELIC_EURUS_CMD_SET_WPA_CFG:
  118. return "SET_WPA_CFG";
  119. case GELIC_EURUS_CMD_GET_WPA_CFG:
  120. return "GET_WPA_CFG";
  121. case GELIC_EURUS_CMD_GET_RSSI_CFG:
  122. return "GET_RSSI";
  123. default:
  124. break;
  125. }
  126. return "";
  127. };
  128. #else
  129. static inline const char *cmdstr(enum gelic_eurus_command ix)
  130. {
  131. return "";
  132. }
  133. #endif
  134. /* synchronously do eurus commands */
  135. static void gelic_eurus_sync_cmd_worker(struct work_struct *work)
  136. {
  137. struct gelic_eurus_cmd *cmd;
  138. struct gelic_card *card;
  139. struct gelic_wl_info *wl;
  140. u64 arg1, arg2;
  141. pr_debug("%s: <-\n", __func__);
  142. cmd = container_of(work, struct gelic_eurus_cmd, work);
  143. BUG_ON(cmd_info[cmd->cmd].pre_arg &&
  144. cmd_info[cmd->cmd].post_arg);
  145. wl = cmd->wl;
  146. card = port_to_card(wl_port(wl));
  147. if (cmd_info[cmd->cmd].pre_arg) {
  148. arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
  149. arg2 = cmd->buf_size;
  150. } else {
  151. arg1 = 0;
  152. arg2 = 0;
  153. }
  154. init_completion(&wl->cmd_done_intr);
  155. pr_debug("%s: cmd='%s' start\n", __func__, cmdstr(cmd->cmd));
  156. cmd->status = lv1_net_control(bus_id(card), dev_id(card),
  157. GELIC_LV1_POST_WLAN_CMD,
  158. cmd->cmd, arg1, arg2,
  159. &cmd->tag, &cmd->size);
  160. if (cmd->status) {
  161. complete(&cmd->done);
  162. pr_info("%s: cmd issue failed\n", __func__);
  163. return;
  164. }
  165. wait_for_completion(&wl->cmd_done_intr);
  166. if (cmd_info[cmd->cmd].post_arg) {
  167. arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
  168. arg2 = cmd->buf_size;
  169. } else {
  170. arg1 = 0;
  171. arg2 = 0;
  172. }
  173. cmd->status = lv1_net_control(bus_id(card), dev_id(card),
  174. GELIC_LV1_GET_WLAN_CMD_RESULT,
  175. cmd->tag, arg1, arg2,
  176. &cmd->cmd_status, &cmd->size);
  177. #ifdef DEBUG
  178. if (cmd->status || cmd->cmd_status) {
  179. pr_debug("%s: cmd done tag=%#lx arg1=%#lx, arg2=%#lx\n", __func__,
  180. cmd->tag, arg1, arg2);
  181. pr_debug("%s: cmd done status=%#x cmd_status=%#lx size=%#lx\n",
  182. __func__, cmd->status, cmd->cmd_status, cmd->size);
  183. }
  184. #endif
  185. complete(&cmd->done);
  186. pr_debug("%s: cmd='%s' done\n", __func__, cmdstr(cmd->cmd));
  187. }
  188. static struct gelic_eurus_cmd *gelic_eurus_sync_cmd(struct gelic_wl_info *wl,
  189. unsigned int eurus_cmd,
  190. void *buffer,
  191. unsigned int buf_size)
  192. {
  193. struct gelic_eurus_cmd *cmd;
  194. /* allocate cmd */
  195. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  196. if (!cmd)
  197. return NULL;
  198. /* initialize members */
  199. cmd->cmd = eurus_cmd;
  200. cmd->buffer = buffer;
  201. cmd->buf_size = buf_size;
  202. cmd->wl = wl;
  203. INIT_WORK(&cmd->work, gelic_eurus_sync_cmd_worker);
  204. init_completion(&cmd->done);
  205. queue_work(wl->eurus_cmd_queue, &cmd->work);
  206. /* wait for command completion */
  207. wait_for_completion(&cmd->done);
  208. return cmd;
  209. }
  210. static u32 gelic_wl_get_link(struct net_device *netdev)
  211. {
  212. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  213. u32 ret;
  214. pr_debug("%s: <-\n", __func__);
  215. mutex_lock(&wl->assoc_stat_lock);
  216. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  217. ret = 1;
  218. else
  219. ret = 0;
  220. mutex_unlock(&wl->assoc_stat_lock);
  221. pr_debug("%s: ->\n", __func__);
  222. return ret;
  223. }
  224. static void gelic_wl_send_iwap_event(struct gelic_wl_info *wl, u8 *bssid)
  225. {
  226. union iwreq_data data;
  227. memset(&data, 0, sizeof(data));
  228. if (bssid)
  229. memcpy(data.ap_addr.sa_data, bssid, ETH_ALEN);
  230. data.ap_addr.sa_family = ARPHRD_ETHER;
  231. wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWAP,
  232. &data, NULL);
  233. }
  234. /*
  235. * wireless extension handlers and helpers
  236. */
  237. /* SIOGIWNAME */
  238. static int gelic_wl_get_name(struct net_device *dev,
  239. struct iw_request_info *info,
  240. union iwreq_data *iwreq, char *extra)
  241. {
  242. strcpy(iwreq->name, "IEEE 802.11bg");
  243. return 0;
  244. }
  245. static void gelic_wl_get_ch_info(struct gelic_wl_info *wl)
  246. {
  247. struct gelic_card *card = port_to_card(wl_port(wl));
  248. u64 ch_info_raw, tmp;
  249. int status;
  250. if (!test_and_set_bit(GELIC_WL_STAT_CH_INFO, &wl->stat)) {
  251. status = lv1_net_control(bus_id(card), dev_id(card),
  252. GELIC_LV1_GET_CHANNEL, 0, 0, 0,
  253. &ch_info_raw,
  254. &tmp);
  255. /* some fw versions may return error */
  256. if (status) {
  257. if (status != LV1_NO_ENTRY)
  258. pr_info("%s: available ch unknown\n", __func__);
  259. wl->ch_info = 0x07ff;/* 11 ch */
  260. } else
  261. /* 16 bits of MSB has available channels */
  262. wl->ch_info = ch_info_raw >> 48;
  263. }
  264. return;
  265. }
  266. /* SIOGIWRANGE */
  267. static int gelic_wl_get_range(struct net_device *netdev,
  268. struct iw_request_info *info,
  269. union iwreq_data *iwreq, char *extra)
  270. {
  271. struct iw_point *point = &iwreq->data;
  272. struct iw_range *range = (struct iw_range *)extra;
  273. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  274. unsigned int i, chs;
  275. pr_debug("%s: <-\n", __func__);
  276. point->length = sizeof(struct iw_range);
  277. memset(range, 0, sizeof(struct iw_range));
  278. range->we_version_compiled = WIRELESS_EXT;
  279. range->we_version_source = 22;
  280. /* available channels and frequencies */
  281. gelic_wl_get_ch_info(wl);
  282. for (i = 0, chs = 0;
  283. i < NUM_CHANNELS && chs < IW_MAX_FREQUENCIES; i++)
  284. if (wl->ch_info & (1 << i)) {
  285. range->freq[chs].i = i + 1;
  286. range->freq[chs].m = channel_freq[i];
  287. range->freq[chs].e = 6;
  288. chs++;
  289. }
  290. range->num_frequency = chs;
  291. range->old_num_frequency = chs;
  292. range->num_channels = chs;
  293. range->old_num_channels = chs;
  294. /* bitrates */
  295. for (i = 0; i < NUM_BITRATES; i++)
  296. range->bitrate[i] = bitrate_list[i];
  297. range->num_bitrates = i;
  298. /* signal levels */
  299. range->max_qual.qual = 100; /* relative value */
  300. range->max_qual.level = 100;
  301. range->avg_qual.qual = 50;
  302. range->avg_qual.level = 50;
  303. range->sensitivity = 0;
  304. /* Event capability */
  305. IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
  306. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
  307. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
  308. /* encryption capability */
  309. range->enc_capa = IW_ENC_CAPA_WPA |
  310. IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
  311. if (wpa2_capable())
  312. range->enc_capa |= IW_ENC_CAPA_WPA2;
  313. range->encoding_size[0] = 5; /* 40bit WEP */
  314. range->encoding_size[1] = 13; /* 104bit WEP */
  315. range->encoding_size[2] = 32; /* WPA-PSK */
  316. range->num_encoding_sizes = 3;
  317. range->max_encoding_tokens = GELIC_WEP_KEYS;
  318. pr_debug("%s: ->\n", __func__);
  319. return 0;
  320. }
  321. /* SIOC{G,S}IWSCAN */
  322. static int gelic_wl_set_scan(struct net_device *netdev,
  323. struct iw_request_info *info,
  324. union iwreq_data *wrqu, char *extra)
  325. {
  326. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  327. return gelic_wl_start_scan(wl, 1);
  328. }
  329. #define OUI_LEN 3
  330. static const u8 rsn_oui[OUI_LEN] = { 0x00, 0x0f, 0xac };
  331. static const u8 wpa_oui[OUI_LEN] = { 0x00, 0x50, 0xf2 };
  332. /*
  333. * synthesize WPA/RSN IE data
  334. * See WiFi WPA specification and IEEE 802.11-2007 7.3.2.25
  335. * for the format
  336. */
  337. static size_t gelic_wl_synthesize_ie(u8 *buf,
  338. struct gelic_eurus_scan_info *scan)
  339. {
  340. const u8 *oui_header;
  341. u8 *start = buf;
  342. int rsn;
  343. int ccmp;
  344. pr_debug("%s: <- sec=%16x\n", __func__, scan->security);
  345. switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_MASK) {
  346. case GELIC_EURUS_SCAN_SEC_WPA:
  347. rsn = 0;
  348. break;
  349. case GELIC_EURUS_SCAN_SEC_WPA2:
  350. rsn = 1;
  351. break;
  352. default:
  353. /* WEP or none. No IE returned */
  354. return 0;
  355. }
  356. switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_WPA_MASK) {
  357. case GELIC_EURUS_SCAN_SEC_WPA_TKIP:
  358. ccmp = 0;
  359. break;
  360. case GELIC_EURUS_SCAN_SEC_WPA_AES:
  361. ccmp = 1;
  362. break;
  363. default:
  364. if (rsn) {
  365. ccmp = 1;
  366. pr_info("%s: no cipher info. defaulted to CCMP\n",
  367. __func__);
  368. } else {
  369. ccmp = 0;
  370. pr_info("%s: no cipher info. defaulted to TKIP\n",
  371. __func__);
  372. }
  373. }
  374. if (rsn)
  375. oui_header = rsn_oui;
  376. else
  377. oui_header = wpa_oui;
  378. /* element id */
  379. if (rsn)
  380. *buf++ = MFIE_TYPE_RSN;
  381. else
  382. *buf++ = MFIE_TYPE_GENERIC;
  383. /* length filed; set later */
  384. buf++;
  385. /* wpa special header */
  386. if (!rsn) {
  387. memcpy(buf, wpa_oui, OUI_LEN);
  388. buf += OUI_LEN;
  389. *buf++ = 0x01;
  390. }
  391. /* version */
  392. *buf++ = 0x01; /* version 1.0 */
  393. *buf++ = 0x00;
  394. /* group cipher */
  395. memcpy(buf, oui_header, OUI_LEN);
  396. buf += OUI_LEN;
  397. if (ccmp)
  398. *buf++ = 0x04; /* CCMP */
  399. else
  400. *buf++ = 0x02; /* TKIP */
  401. /* pairwise key count always 1 */
  402. *buf++ = 0x01;
  403. *buf++ = 0x00;
  404. /* pairwise key suit */
  405. memcpy(buf, oui_header, OUI_LEN);
  406. buf += OUI_LEN;
  407. if (ccmp)
  408. *buf++ = 0x04; /* CCMP */
  409. else
  410. *buf++ = 0x02; /* TKIP */
  411. /* AKM count is 1 */
  412. *buf++ = 0x01;
  413. *buf++ = 0x00;
  414. /* AKM suite is assumed as PSK*/
  415. memcpy(buf, oui_header, OUI_LEN);
  416. buf += OUI_LEN;
  417. *buf++ = 0x02; /* PSK */
  418. /* RSN capabilities is 0 */
  419. *buf++ = 0x00;
  420. *buf++ = 0x00;
  421. /* set length field */
  422. start[1] = (buf - start - 2);
  423. pr_debug("%s: ->\n", __func__);
  424. return (buf - start);
  425. }
  426. struct ie_item {
  427. u8 *data;
  428. u8 len;
  429. };
  430. struct ie_info {
  431. struct ie_item wpa;
  432. struct ie_item rsn;
  433. };
  434. static void gelic_wl_parse_ie(u8 *data, size_t len,
  435. struct ie_info *ie_info)
  436. {
  437. size_t data_left = len;
  438. u8 *pos = data;
  439. u8 item_len;
  440. u8 item_id;
  441. pr_debug("%s: data=%p len=%ld \n", __func__,
  442. data, len);
  443. memset(ie_info, 0, sizeof(struct ie_info));
  444. while (2 <= data_left) {
  445. item_id = *pos++;
  446. item_len = *pos++;
  447. data_left -= 2;
  448. if (data_left < item_len)
  449. break;
  450. switch (item_id) {
  451. case MFIE_TYPE_GENERIC:
  452. if ((OUI_LEN + 1 <= item_len) &&
  453. !memcmp(pos, wpa_oui, OUI_LEN) &&
  454. pos[OUI_LEN] == 0x01) {
  455. ie_info->wpa.data = pos - 2;
  456. ie_info->wpa.len = item_len + 2;
  457. }
  458. break;
  459. case MFIE_TYPE_RSN:
  460. ie_info->rsn.data = pos - 2;
  461. /* length includes the header */
  462. ie_info->rsn.len = item_len + 2;
  463. break;
  464. default:
  465. pr_debug("%s: ignore %#x,%d\n", __func__,
  466. item_id, item_len);
  467. break;
  468. }
  469. pos += item_len;
  470. data_left -= item_len;
  471. }
  472. pr_debug("%s: wpa=%p,%d wpa2=%p,%d\n", __func__,
  473. ie_info->wpa.data, ie_info->wpa.len,
  474. ie_info->rsn.data, ie_info->rsn.len);
  475. }
  476. /*
  477. * translate the scan informations from hypervisor to a
  478. * independent format
  479. */
  480. static char *gelic_wl_translate_scan(struct net_device *netdev,
  481. char *ev,
  482. char *stop,
  483. struct gelic_wl_scan_info *network)
  484. {
  485. struct iw_event iwe;
  486. struct gelic_eurus_scan_info *scan = network->hwinfo;
  487. char *tmp;
  488. u8 rate;
  489. unsigned int i, j, len;
  490. u8 buf[MAX_WPA_IE_LEN];
  491. pr_debug("%s: <-\n", __func__);
  492. /* first entry should be AP's mac address */
  493. iwe.cmd = SIOCGIWAP;
  494. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  495. memcpy(iwe.u.ap_addr.sa_data, &scan->bssid[2], ETH_ALEN);
  496. ev = iwe_stream_add_event(ev, stop, &iwe, IW_EV_ADDR_LEN);
  497. /* ESSID */
  498. iwe.cmd = SIOCGIWESSID;
  499. iwe.u.data.flags = 1;
  500. iwe.u.data.length = strnlen(scan->essid, 32);
  501. ev = iwe_stream_add_point(ev, stop, &iwe, scan->essid);
  502. /* FREQUENCY */
  503. iwe.cmd = SIOCGIWFREQ;
  504. iwe.u.freq.m = be16_to_cpu(scan->channel);
  505. iwe.u.freq.e = 0; /* table value in MHz */
  506. iwe.u.freq.i = 0;
  507. ev = iwe_stream_add_event(ev, stop, &iwe, IW_EV_FREQ_LEN);
  508. /* RATES */
  509. iwe.cmd = SIOCGIWRATE;
  510. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  511. /* to stuff multiple values in one event */
  512. tmp = ev + IW_EV_LCP_LEN;
  513. /* put them in ascendant order (older is first) */
  514. i = 0;
  515. j = 0;
  516. pr_debug("%s: rates=%d rate=%d\n", __func__,
  517. network->rate_len, network->rate_ext_len);
  518. while (i < network->rate_len) {
  519. if (j < network->rate_ext_len &&
  520. ((scan->ext_rate[j] & 0x7f) < (scan->rate[i] & 0x7f)))
  521. rate = scan->ext_rate[j++] & 0x7f;
  522. else
  523. rate = scan->rate[i++] & 0x7f;
  524. iwe.u.bitrate.value = rate * 500000; /* 500kbps unit */
  525. tmp = iwe_stream_add_value(ev, tmp, stop, &iwe,
  526. IW_EV_PARAM_LEN);
  527. }
  528. while (j < network->rate_ext_len) {
  529. iwe.u.bitrate.value = (scan->ext_rate[j++] & 0x7f) * 500000;
  530. tmp = iwe_stream_add_value(ev, tmp, stop, &iwe,
  531. IW_EV_PARAM_LEN);
  532. }
  533. /* Check if we added any rate */
  534. if (IW_EV_LCP_LEN < (tmp - ev))
  535. ev = tmp;
  536. /* ENCODE */
  537. iwe.cmd = SIOCGIWENCODE;
  538. if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_PRIVACY)
  539. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  540. else
  541. iwe.u.data.flags = IW_ENCODE_DISABLED;
  542. iwe.u.data.length = 0;
  543. ev = iwe_stream_add_point(ev, stop, &iwe, scan->essid);
  544. /* MODE */
  545. iwe.cmd = SIOCGIWMODE;
  546. if (be16_to_cpu(scan->capability) &
  547. (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
  548. if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_ESS)
  549. iwe.u.mode = IW_MODE_MASTER;
  550. else
  551. iwe.u.mode = IW_MODE_ADHOC;
  552. ev = iwe_stream_add_event(ev, stop, &iwe, IW_EV_UINT_LEN);
  553. }
  554. /* QUAL */
  555. iwe.cmd = IWEVQUAL;
  556. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED |
  557. IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
  558. iwe.u.qual.level = be16_to_cpu(scan->rssi);
  559. iwe.u.qual.qual = be16_to_cpu(scan->rssi);
  560. iwe.u.qual.noise = 0;
  561. ev = iwe_stream_add_event(ev, stop, &iwe, IW_EV_QUAL_LEN);
  562. /* RSN */
  563. memset(&iwe, 0, sizeof(iwe));
  564. if (be16_to_cpu(scan->size) <= sizeof(*scan)) {
  565. /* If wpa[2] capable station, synthesize IE and put it */
  566. len = gelic_wl_synthesize_ie(buf, scan);
  567. if (len) {
  568. iwe.cmd = IWEVGENIE;
  569. iwe.u.data.length = len;
  570. ev = iwe_stream_add_point(ev, stop, &iwe, buf);
  571. }
  572. } else {
  573. /* this scan info has IE data */
  574. struct ie_info ie_info;
  575. size_t data_len;
  576. data_len = be16_to_cpu(scan->size) - sizeof(*scan);
  577. gelic_wl_parse_ie(scan->elements, data_len, &ie_info);
  578. if (ie_info.wpa.len && (ie_info.wpa.len <= sizeof(buf))) {
  579. memcpy(buf, ie_info.wpa.data, ie_info.wpa.len);
  580. iwe.cmd = IWEVGENIE;
  581. iwe.u.data.length = ie_info.wpa.len;
  582. ev = iwe_stream_add_point(ev, stop, &iwe, buf);
  583. }
  584. if (ie_info.rsn.len && (ie_info.rsn.len <= sizeof(buf))) {
  585. memset(&iwe, 0, sizeof(iwe));
  586. memcpy(buf, ie_info.rsn.data, ie_info.rsn.len);
  587. iwe.cmd = IWEVGENIE;
  588. iwe.u.data.length = ie_info.rsn.len;
  589. ev = iwe_stream_add_point(ev, stop, &iwe, buf);
  590. }
  591. }
  592. pr_debug("%s: ->\n", __func__);
  593. return ev;
  594. }
  595. static int gelic_wl_get_scan(struct net_device *netdev,
  596. struct iw_request_info *info,
  597. union iwreq_data *wrqu, char *extra)
  598. {
  599. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  600. struct gelic_wl_scan_info *scan_info;
  601. char *ev = extra;
  602. char *stop = ev + wrqu->data.length;
  603. int ret = 0;
  604. unsigned long this_time = jiffies;
  605. pr_debug("%s: <-\n", __func__);
  606. if (mutex_lock_interruptible(&wl->scan_lock))
  607. return -EAGAIN;
  608. switch (wl->scan_stat) {
  609. case GELIC_WL_SCAN_STAT_SCANNING:
  610. /* If a scan in progress, caller should call me again */
  611. ret = -EAGAIN;
  612. goto out;
  613. break;
  614. case GELIC_WL_SCAN_STAT_INIT:
  615. /* last scan request failed or never issued */
  616. ret = -ENODEV;
  617. goto out;
  618. break;
  619. case GELIC_WL_SCAN_STAT_GOT_LIST:
  620. /* ok, use current list */
  621. break;
  622. }
  623. list_for_each_entry(scan_info, &wl->network_list, list) {
  624. if (wl->scan_age == 0 ||
  625. time_after(scan_info->last_scanned + wl->scan_age,
  626. this_time))
  627. ev = gelic_wl_translate_scan(netdev, ev, stop,
  628. scan_info);
  629. else
  630. pr_debug("%s:entry too old\n", __func__);
  631. if (stop - ev <= IW_EV_ADDR_LEN) {
  632. ret = -E2BIG;
  633. goto out;
  634. }
  635. }
  636. wrqu->data.length = ev - extra;
  637. wrqu->data.flags = 0;
  638. out:
  639. mutex_unlock(&wl->scan_lock);
  640. pr_debug("%s: -> %d %d\n", __func__, ret, wrqu->data.length);
  641. return ret;
  642. }
  643. #ifdef DEBUG
  644. static void scan_list_dump(struct gelic_wl_info *wl)
  645. {
  646. struct gelic_wl_scan_info *scan_info;
  647. int i;
  648. DECLARE_MAC_BUF(mac);
  649. i = 0;
  650. list_for_each_entry(scan_info, &wl->network_list, list) {
  651. pr_debug("%s: item %d\n", __func__, i++);
  652. pr_debug("valid=%d eurusindex=%d last=%lx\n",
  653. scan_info->valid, scan_info->eurus_index,
  654. scan_info->last_scanned);
  655. pr_debug("r_len=%d r_ext_len=%d essid_len=%d\n",
  656. scan_info->rate_len, scan_info->rate_ext_len,
  657. scan_info->essid_len);
  658. /* -- */
  659. pr_debug("bssid=%s\n",
  660. print_mac(mac, &scan_info->hwinfo->bssid[2]));
  661. pr_debug("essid=%s\n", scan_info->hwinfo->essid);
  662. }
  663. }
  664. #endif
  665. static int gelic_wl_set_auth(struct net_device *netdev,
  666. struct iw_request_info *info,
  667. union iwreq_data *data, char *extra)
  668. {
  669. struct iw_param *param = &data->param;
  670. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  671. unsigned long irqflag;
  672. int ret = 0;
  673. pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
  674. spin_lock_irqsave(&wl->lock, irqflag);
  675. switch (param->flags & IW_AUTH_INDEX) {
  676. case IW_AUTH_WPA_VERSION:
  677. if (param->value & IW_AUTH_WPA_VERSION_DISABLED) {
  678. pr_debug("%s: NO WPA selected\n", __func__);
  679. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  680. wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
  681. wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
  682. }
  683. if (param->value & IW_AUTH_WPA_VERSION_WPA) {
  684. pr_debug("%s: WPA version 1 selected\n", __func__);
  685. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
  686. wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
  687. wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
  688. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  689. }
  690. if (param->value & IW_AUTH_WPA_VERSION_WPA2) {
  691. /*
  692. * As the hypervisor may not tell the cipher
  693. * information of the AP if it is WPA2,
  694. * you will not decide suitable cipher from
  695. * its beacon.
  696. * You should have knowledge about the AP's
  697. * cipher infomation in other method prior to
  698. * the association.
  699. */
  700. if (!precise_ie())
  701. pr_info("%s: WPA2 may not work\n", __func__);
  702. if (wpa2_capable()) {
  703. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA2;
  704. wl->group_cipher_method = GELIC_WL_CIPHER_AES;
  705. wl->pairwise_cipher_method =
  706. GELIC_WL_CIPHER_AES;
  707. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  708. } else
  709. ret = -EINVAL;
  710. }
  711. break;
  712. case IW_AUTH_CIPHER_PAIRWISE:
  713. if (param->value &
  714. (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
  715. pr_debug("%s: WEP selected\n", __func__);
  716. wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
  717. }
  718. if (param->value & IW_AUTH_CIPHER_TKIP) {
  719. pr_debug("%s: TKIP selected\n", __func__);
  720. wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
  721. }
  722. if (param->value & IW_AUTH_CIPHER_CCMP) {
  723. pr_debug("%s: CCMP selected\n", __func__);
  724. wl->pairwise_cipher_method = GELIC_WL_CIPHER_AES;
  725. }
  726. if (param->value & IW_AUTH_CIPHER_NONE) {
  727. pr_debug("%s: no auth selected\n", __func__);
  728. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  729. }
  730. break;
  731. case IW_AUTH_CIPHER_GROUP:
  732. if (param->value &
  733. (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
  734. pr_debug("%s: WEP selected\n", __func__);
  735. wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
  736. }
  737. if (param->value & IW_AUTH_CIPHER_TKIP) {
  738. pr_debug("%s: TKIP selected\n", __func__);
  739. wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
  740. }
  741. if (param->value & IW_AUTH_CIPHER_CCMP) {
  742. pr_debug("%s: CCMP selected\n", __func__);
  743. wl->group_cipher_method = GELIC_WL_CIPHER_AES;
  744. }
  745. if (param->value & IW_AUTH_CIPHER_NONE) {
  746. pr_debug("%s: no auth selected\n", __func__);
  747. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  748. }
  749. break;
  750. case IW_AUTH_80211_AUTH_ALG:
  751. if (param->value & IW_AUTH_ALG_SHARED_KEY) {
  752. pr_debug("%s: shared key specified\n", __func__);
  753. wl->auth_method = GELIC_EURUS_AUTH_SHARED;
  754. } else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
  755. pr_debug("%s: open system specified\n", __func__);
  756. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  757. } else
  758. ret = -EINVAL;
  759. break;
  760. case IW_AUTH_WPA_ENABLED:
  761. if (param->value) {
  762. pr_debug("%s: WPA enabled\n", __func__);
  763. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
  764. } else {
  765. pr_debug("%s: WPA disabled\n", __func__);
  766. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  767. }
  768. break;
  769. case IW_AUTH_KEY_MGMT:
  770. if (param->value & IW_AUTH_KEY_MGMT_PSK)
  771. break;
  772. /* intentionally fall through */
  773. default:
  774. ret = -EOPNOTSUPP;
  775. break;
  776. };
  777. if (!ret)
  778. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  779. spin_unlock_irqrestore(&wl->lock, irqflag);
  780. pr_debug("%s: -> %d\n", __func__, ret);
  781. return ret;
  782. }
  783. static int gelic_wl_get_auth(struct net_device *netdev,
  784. struct iw_request_info *info,
  785. union iwreq_data *iwreq, char *extra)
  786. {
  787. struct iw_param *param = &iwreq->param;
  788. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  789. unsigned long irqflag;
  790. int ret = 0;
  791. pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
  792. spin_lock_irqsave(&wl->lock, irqflag);
  793. switch (param->flags & IW_AUTH_INDEX) {
  794. case IW_AUTH_WPA_VERSION:
  795. switch (wl->wpa_level) {
  796. case GELIC_WL_WPA_LEVEL_WPA:
  797. param->value |= IW_AUTH_WPA_VERSION_WPA;
  798. break;
  799. case GELIC_WL_WPA_LEVEL_WPA2:
  800. param->value |= IW_AUTH_WPA_VERSION_WPA2;
  801. break;
  802. default:
  803. param->value |= IW_AUTH_WPA_VERSION_DISABLED;
  804. }
  805. break;
  806. case IW_AUTH_80211_AUTH_ALG:
  807. if (wl->auth_method == GELIC_EURUS_AUTH_SHARED)
  808. param->value = IW_AUTH_ALG_SHARED_KEY;
  809. else if (wl->auth_method == GELIC_EURUS_AUTH_OPEN)
  810. param->value = IW_AUTH_ALG_OPEN_SYSTEM;
  811. break;
  812. case IW_AUTH_WPA_ENABLED:
  813. switch (wl->wpa_level) {
  814. case GELIC_WL_WPA_LEVEL_WPA:
  815. case GELIC_WL_WPA_LEVEL_WPA2:
  816. param->value = 1;
  817. break;
  818. default:
  819. param->value = 0;
  820. break;
  821. }
  822. break;
  823. default:
  824. ret = -EOPNOTSUPP;
  825. }
  826. spin_unlock_irqrestore(&wl->lock, irqflag);
  827. pr_debug("%s: -> %d\n", __func__, ret);
  828. return ret;
  829. }
  830. /* SIOC{S,G}IWESSID */
  831. static int gelic_wl_set_essid(struct net_device *netdev,
  832. struct iw_request_info *info,
  833. union iwreq_data *data, char *extra)
  834. {
  835. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  836. unsigned long irqflag;
  837. pr_debug("%s: <- l=%d f=%d\n", __func__,
  838. data->essid.length, data->essid.flags);
  839. if (IW_ESSID_MAX_SIZE < data->essid.length)
  840. return -EINVAL;
  841. spin_lock_irqsave(&wl->lock, irqflag);
  842. if (data->essid.flags) {
  843. wl->essid_len = data->essid.length;
  844. memcpy(wl->essid, extra, wl->essid_len);
  845. pr_debug("%s: essid = '%s'\n", __func__, extra);
  846. set_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
  847. } else {
  848. pr_debug("%s: ESSID any \n", __func__);
  849. clear_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
  850. }
  851. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  852. spin_unlock_irqrestore(&wl->lock, irqflag);
  853. gelic_wl_try_associate(netdev); /* FIXME */
  854. pr_debug("%s: -> \n", __func__);
  855. return 0;
  856. }
  857. static int gelic_wl_get_essid(struct net_device *netdev,
  858. struct iw_request_info *info,
  859. union iwreq_data *data, char *extra)
  860. {
  861. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  862. unsigned long irqflag;
  863. pr_debug("%s: <- \n", __func__);
  864. mutex_lock(&wl->assoc_stat_lock);
  865. spin_lock_irqsave(&wl->lock, irqflag);
  866. if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat) ||
  867. wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
  868. memcpy(extra, wl->essid, wl->essid_len);
  869. data->essid.length = wl->essid_len;
  870. data->essid.flags = 1;
  871. } else
  872. data->essid.flags = 0;
  873. mutex_unlock(&wl->assoc_stat_lock);
  874. spin_unlock_irqrestore(&wl->lock, irqflag);
  875. pr_debug("%s: -> len=%d \n", __func__, data->essid.length);
  876. return 0;
  877. }
  878. /* SIO{S,G}IWENCODE */
  879. static int gelic_wl_set_encode(struct net_device *netdev,
  880. struct iw_request_info *info,
  881. union iwreq_data *data, char *extra)
  882. {
  883. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  884. struct iw_point *enc = &data->encoding;
  885. __u16 flags;
  886. unsigned int irqflag;
  887. int key_index, index_specified;
  888. int ret = 0;
  889. pr_debug("%s: <- \n", __func__);
  890. flags = enc->flags & IW_ENCODE_FLAGS;
  891. key_index = enc->flags & IW_ENCODE_INDEX;
  892. pr_debug("%s: key_index = %d\n", __func__, key_index);
  893. pr_debug("%s: key_len = %d\n", __func__, enc->length);
  894. pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
  895. if (GELIC_WEP_KEYS < key_index)
  896. return -EINVAL;
  897. spin_lock_irqsave(&wl->lock, irqflag);
  898. if (key_index) {
  899. index_specified = 1;
  900. key_index--;
  901. } else {
  902. index_specified = 0;
  903. key_index = wl->current_key;
  904. }
  905. if (flags & IW_ENCODE_NOKEY) {
  906. /* if just IW_ENCODE_NOKEY, change current key index */
  907. if (!flags && index_specified) {
  908. wl->current_key = key_index;
  909. goto done;
  910. }
  911. if (flags & IW_ENCODE_DISABLED) {
  912. if (!index_specified) {
  913. /* disable encryption */
  914. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  915. wl->pairwise_cipher_method =
  916. GELIC_WL_CIPHER_NONE;
  917. /* invalidate all key */
  918. wl->key_enabled = 0;
  919. } else
  920. clear_bit(key_index, &wl->key_enabled);
  921. }
  922. if (flags & IW_ENCODE_OPEN)
  923. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  924. if (flags & IW_ENCODE_RESTRICTED) {
  925. pr_info("%s: shared key mode enabled\n", __func__);
  926. wl->auth_method = GELIC_EURUS_AUTH_SHARED;
  927. }
  928. } else {
  929. if (IW_ENCODING_TOKEN_MAX < enc->length) {
  930. ret = -EINVAL;
  931. goto done;
  932. }
  933. wl->key_len[key_index] = enc->length;
  934. memcpy(wl->key[key_index], extra, enc->length);
  935. set_bit(key_index, &wl->key_enabled);
  936. wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
  937. wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
  938. }
  939. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  940. done:
  941. spin_unlock_irqrestore(&wl->lock, irqflag);
  942. pr_debug("%s: -> \n", __func__);
  943. return ret;
  944. }
  945. static int gelic_wl_get_encode(struct net_device *netdev,
  946. struct iw_request_info *info,
  947. union iwreq_data *data, char *extra)
  948. {
  949. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  950. struct iw_point *enc = &data->encoding;
  951. unsigned int irqflag;
  952. unsigned int key_index, index_specified;
  953. int ret = 0;
  954. pr_debug("%s: <- \n", __func__);
  955. key_index = enc->flags & IW_ENCODE_INDEX;
  956. pr_debug("%s: flag=%#x point=%p len=%d extra=%p\n", __func__,
  957. enc->flags, enc->pointer, enc->length, extra);
  958. if (GELIC_WEP_KEYS < key_index)
  959. return -EINVAL;
  960. spin_lock_irqsave(&wl->lock, irqflag);
  961. if (key_index) {
  962. index_specified = 1;
  963. key_index--;
  964. } else {
  965. index_specified = 0;
  966. key_index = wl->current_key;
  967. }
  968. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  969. switch (wl->auth_method) {
  970. case GELIC_EURUS_AUTH_OPEN:
  971. enc->flags = IW_ENCODE_OPEN;
  972. break;
  973. case GELIC_EURUS_AUTH_SHARED:
  974. enc->flags = IW_ENCODE_RESTRICTED;
  975. break;
  976. }
  977. } else
  978. enc->flags = IW_ENCODE_DISABLED;
  979. if (test_bit(key_index, &wl->key_enabled)) {
  980. if (enc->length < wl->key_len[key_index]) {
  981. ret = -EINVAL;
  982. goto done;
  983. }
  984. enc->length = wl->key_len[key_index];
  985. memcpy(extra, wl->key[key_index], wl->key_len[key_index]);
  986. } else {
  987. enc->length = 0;
  988. enc->flags |= IW_ENCODE_NOKEY;
  989. }
  990. enc->flags |= key_index + 1;
  991. pr_debug("%s: -> flag=%x len=%d\n", __func__,
  992. enc->flags, enc->length);
  993. done:
  994. spin_unlock_irqrestore(&wl->lock, irqflag);
  995. return ret;
  996. }
  997. /* SIOC{S,G}IWAP */
  998. static int gelic_wl_set_ap(struct net_device *netdev,
  999. struct iw_request_info *info,
  1000. union iwreq_data *data, char *extra)
  1001. {
  1002. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1003. unsigned long irqflag;
  1004. pr_debug("%s: <-\n", __func__);
  1005. if (data->ap_addr.sa_family != ARPHRD_ETHER)
  1006. return -EINVAL;
  1007. spin_lock_irqsave(&wl->lock, irqflag);
  1008. if (is_valid_ether_addr(data->ap_addr.sa_data)) {
  1009. memcpy(wl->bssid, data->ap_addr.sa_data,
  1010. ETH_ALEN);
  1011. set_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
  1012. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  1013. pr_debug("%s: bss=%02x:%02x:%02x:%02x:%02x:%02x\n",
  1014. __func__,
  1015. wl->bssid[0], wl->bssid[1],
  1016. wl->bssid[2], wl->bssid[3],
  1017. wl->bssid[4], wl->bssid[5]);
  1018. } else {
  1019. pr_debug("%s: clear bssid\n", __func__);
  1020. clear_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
  1021. memset(wl->bssid, 0, ETH_ALEN);
  1022. }
  1023. spin_unlock_irqrestore(&wl->lock, irqflag);
  1024. pr_debug("%s: ->\n", __func__);
  1025. return 0;
  1026. }
  1027. static int gelic_wl_get_ap(struct net_device *netdev,
  1028. struct iw_request_info *info,
  1029. union iwreq_data *data, char *extra)
  1030. {
  1031. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1032. unsigned long irqflag;
  1033. pr_debug("%s: <-\n", __func__);
  1034. mutex_lock(&wl->assoc_stat_lock);
  1035. spin_lock_irqsave(&wl->lock, irqflag);
  1036. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
  1037. data->ap_addr.sa_family = ARPHRD_ETHER;
  1038. memcpy(data->ap_addr.sa_data, wl->active_bssid,
  1039. ETH_ALEN);
  1040. } else
  1041. memset(data->ap_addr.sa_data, 0, ETH_ALEN);
  1042. spin_unlock_irqrestore(&wl->lock, irqflag);
  1043. mutex_unlock(&wl->assoc_stat_lock);
  1044. pr_debug("%s: ->\n", __func__);
  1045. return 0;
  1046. }
  1047. /* SIOC{S,G}IWENCODEEXT */
  1048. static int gelic_wl_set_encodeext(struct net_device *netdev,
  1049. struct iw_request_info *info,
  1050. union iwreq_data *data, char *extra)
  1051. {
  1052. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1053. struct iw_point *enc = &data->encoding;
  1054. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1055. __u16 alg;
  1056. __u16 flags;
  1057. unsigned int irqflag;
  1058. int key_index;
  1059. int ret = 0;
  1060. pr_debug("%s: <- \n", __func__);
  1061. flags = enc->flags & IW_ENCODE_FLAGS;
  1062. alg = ext->alg;
  1063. key_index = enc->flags & IW_ENCODE_INDEX;
  1064. pr_debug("%s: key_index = %d\n", __func__, key_index);
  1065. pr_debug("%s: key_len = %d\n", __func__, enc->length);
  1066. pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
  1067. pr_debug("%s: ext_flag=%x\n", __func__, ext->ext_flags);
  1068. pr_debug("%s: ext_key_len=%x\n", __func__, ext->key_len);
  1069. if (GELIC_WEP_KEYS < key_index)
  1070. return -EINVAL;
  1071. spin_lock_irqsave(&wl->lock, irqflag);
  1072. if (key_index)
  1073. key_index--;
  1074. else
  1075. key_index = wl->current_key;
  1076. if (!enc->length && (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)) {
  1077. /* reques to change default key index */
  1078. pr_debug("%s: request to change default key to %d\n",
  1079. __func__, key_index);
  1080. wl->current_key = key_index;
  1081. goto done;
  1082. }
  1083. if (alg == IW_ENCODE_ALG_NONE || (flags & IW_ENCODE_DISABLED)) {
  1084. pr_debug("%s: alg disabled\n", __func__);
  1085. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  1086. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  1087. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  1088. wl->auth_method = GELIC_EURUS_AUTH_OPEN; /* should be open */
  1089. } else if (alg == IW_ENCODE_ALG_WEP) {
  1090. pr_debug("%s: WEP requested\n", __func__);
  1091. if (flags & IW_ENCODE_OPEN) {
  1092. pr_debug("%s: open key mode\n", __func__);
  1093. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  1094. }
  1095. if (flags & IW_ENCODE_RESTRICTED) {
  1096. pr_debug("%s: shared key mode\n", __func__);
  1097. wl->auth_method = GELIC_EURUS_AUTH_SHARED;
  1098. }
  1099. if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
  1100. pr_info("%s: key is too long %d\n", __func__,
  1101. ext->key_len);
  1102. ret = -EINVAL;
  1103. goto done;
  1104. }
  1105. /* OK, update the key */
  1106. wl->key_len[key_index] = ext->key_len;
  1107. memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
  1108. memcpy(wl->key[key_index], ext->key, ext->key_len);
  1109. set_bit(key_index, &wl->key_enabled);
  1110. /* remember wep info changed */
  1111. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  1112. } else if ((alg == IW_ENCODE_ALG_TKIP) || (alg == IW_ENCODE_ALG_CCMP)) {
  1113. pr_debug("%s: TKIP/CCMP requested alg=%d\n", __func__, alg);
  1114. /* check key length */
  1115. if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
  1116. pr_info("%s: key is too long %d\n", __func__,
  1117. ext->key_len);
  1118. ret = -EINVAL;
  1119. goto done;
  1120. }
  1121. if (alg == IW_ENCODE_ALG_CCMP) {
  1122. pr_debug("%s: AES selected\n", __func__);
  1123. wl->group_cipher_method = GELIC_WL_CIPHER_AES;
  1124. wl->pairwise_cipher_method = GELIC_WL_CIPHER_AES;
  1125. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA2;
  1126. } else {
  1127. pr_debug("%s: TKIP selected, WPA forced\n", __func__);
  1128. wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
  1129. wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
  1130. /* FIXME: how do we do if WPA2 + TKIP? */
  1131. wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
  1132. }
  1133. if (flags & IW_ENCODE_RESTRICTED)
  1134. BUG();
  1135. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  1136. /* We should use same key for both and unicast */
  1137. if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
  1138. pr_debug("%s: group key \n", __func__);
  1139. else
  1140. pr_debug("%s: unicast key \n", __func__);
  1141. /* OK, update the key */
  1142. wl->key_len[key_index] = ext->key_len;
  1143. memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
  1144. memcpy(wl->key[key_index], ext->key, ext->key_len);
  1145. set_bit(key_index, &wl->key_enabled);
  1146. /* remember info changed */
  1147. set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
  1148. }
  1149. done:
  1150. spin_unlock_irqrestore(&wl->lock, irqflag);
  1151. pr_debug("%s: -> \n", __func__);
  1152. return ret;
  1153. }
  1154. static int gelic_wl_get_encodeext(struct net_device *netdev,
  1155. struct iw_request_info *info,
  1156. union iwreq_data *data, char *extra)
  1157. {
  1158. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1159. struct iw_point *enc = &data->encoding;
  1160. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  1161. unsigned int irqflag;
  1162. int key_index;
  1163. int ret = 0;
  1164. int max_key_len;
  1165. pr_debug("%s: <- \n", __func__);
  1166. max_key_len = enc->length - sizeof(struct iw_encode_ext);
  1167. if (max_key_len < 0)
  1168. return -EINVAL;
  1169. key_index = enc->flags & IW_ENCODE_INDEX;
  1170. pr_debug("%s: key_index = %d\n", __func__, key_index);
  1171. pr_debug("%s: key_len = %d\n", __func__, enc->length);
  1172. pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
  1173. if (GELIC_WEP_KEYS < key_index)
  1174. return -EINVAL;
  1175. spin_lock_irqsave(&wl->lock, irqflag);
  1176. if (key_index)
  1177. key_index--;
  1178. else
  1179. key_index = wl->current_key;
  1180. memset(ext, 0, sizeof(struct iw_encode_ext));
  1181. switch (wl->group_cipher_method) {
  1182. case GELIC_WL_CIPHER_WEP:
  1183. ext->alg = IW_ENCODE_ALG_WEP;
  1184. enc->flags |= IW_ENCODE_ENABLED;
  1185. break;
  1186. case GELIC_WL_CIPHER_TKIP:
  1187. ext->alg = IW_ENCODE_ALG_TKIP;
  1188. enc->flags |= IW_ENCODE_ENABLED;
  1189. break;
  1190. case GELIC_WL_CIPHER_AES:
  1191. ext->alg = IW_ENCODE_ALG_CCMP;
  1192. enc->flags |= IW_ENCODE_ENABLED;
  1193. break;
  1194. case GELIC_WL_CIPHER_NONE:
  1195. default:
  1196. ext->alg = IW_ENCODE_ALG_NONE;
  1197. enc->flags |= IW_ENCODE_NOKEY;
  1198. break;
  1199. }
  1200. if (!(enc->flags & IW_ENCODE_NOKEY)) {
  1201. if (max_key_len < wl->key_len[key_index]) {
  1202. ret = -E2BIG;
  1203. goto out;
  1204. }
  1205. if (test_bit(key_index, &wl->key_enabled))
  1206. memcpy(ext->key, wl->key[key_index],
  1207. wl->key_len[key_index]);
  1208. else
  1209. pr_debug("%s: disabled key requested ix=%d\n",
  1210. __func__, key_index);
  1211. }
  1212. out:
  1213. spin_unlock_irqrestore(&wl->lock, irqflag);
  1214. pr_debug("%s: -> \n", __func__);
  1215. return ret;
  1216. }
  1217. /* SIOC{S,G}IWMODE */
  1218. static int gelic_wl_set_mode(struct net_device *netdev,
  1219. struct iw_request_info *info,
  1220. union iwreq_data *data, char *extra)
  1221. {
  1222. __u32 mode = data->mode;
  1223. int ret;
  1224. pr_debug("%s: <- \n", __func__);
  1225. if (mode == IW_MODE_INFRA)
  1226. ret = 0;
  1227. else
  1228. ret = -EOPNOTSUPP;
  1229. pr_debug("%s: -> %d\n", __func__, ret);
  1230. return ret;
  1231. }
  1232. static int gelic_wl_get_mode(struct net_device *netdev,
  1233. struct iw_request_info *info,
  1234. union iwreq_data *data, char *extra)
  1235. {
  1236. __u32 *mode = &data->mode;
  1237. pr_debug("%s: <- \n", __func__);
  1238. *mode = IW_MODE_INFRA;
  1239. pr_debug("%s: ->\n", __func__);
  1240. return 0;
  1241. }
  1242. /* SIOCIWFIRSTPRIV */
  1243. static int hex2bin(u8 *str, u8 *bin, unsigned int len)
  1244. {
  1245. unsigned int i;
  1246. static unsigned char *hex = "0123456789ABCDEF";
  1247. unsigned char *p, *q;
  1248. u8 tmp;
  1249. if (len != WPA_PSK_LEN * 2)
  1250. return -EINVAL;
  1251. for (i = 0; i < WPA_PSK_LEN * 2; i += 2) {
  1252. p = strchr(hex, toupper(str[i]));
  1253. q = strchr(hex, toupper(str[i + 1]));
  1254. if (!p || !q) {
  1255. pr_info("%s: unconvertible PSK digit=%d\n",
  1256. __func__, i);
  1257. return -EINVAL;
  1258. }
  1259. tmp = ((p - hex) << 4) + (q - hex);
  1260. *bin++ = tmp;
  1261. }
  1262. return 0;
  1263. };
  1264. static int gelic_wl_priv_set_psk(struct net_device *net_dev,
  1265. struct iw_request_info *info,
  1266. union iwreq_data *data, char *extra)
  1267. {
  1268. struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
  1269. unsigned int len;
  1270. unsigned int irqflag;
  1271. int ret = 0;
  1272. pr_debug("%s:<- len=%d\n", __func__, data->data.length);
  1273. len = data->data.length - 1;
  1274. if (len <= 2)
  1275. return -EINVAL;
  1276. spin_lock_irqsave(&wl->lock, irqflag);
  1277. if (extra[0] == '"' && extra[len - 1] == '"') {
  1278. pr_debug("%s: passphrase mode\n", __func__);
  1279. /* pass phrase */
  1280. if (GELIC_WL_EURUS_PSK_MAX_LEN < (len - 2)) {
  1281. pr_info("%s: passphrase too long\n", __func__);
  1282. ret = -E2BIG;
  1283. goto out;
  1284. }
  1285. memset(wl->psk, 0, sizeof(wl->psk));
  1286. wl->psk_len = len - 2;
  1287. memcpy(wl->psk, &(extra[1]), wl->psk_len);
  1288. wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
  1289. } else {
  1290. ret = hex2bin(extra, wl->psk, len);
  1291. if (ret)
  1292. goto out;
  1293. wl->psk_len = WPA_PSK_LEN;
  1294. wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
  1295. }
  1296. set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
  1297. out:
  1298. spin_unlock_irqrestore(&wl->lock, irqflag);
  1299. pr_debug("%s:->\n", __func__);
  1300. return ret;
  1301. }
  1302. static int gelic_wl_priv_get_psk(struct net_device *net_dev,
  1303. struct iw_request_info *info,
  1304. union iwreq_data *data, char *extra)
  1305. {
  1306. struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
  1307. char *p;
  1308. unsigned int irqflag;
  1309. unsigned int i;
  1310. pr_debug("%s:<-\n", __func__);
  1311. if (!capable(CAP_NET_ADMIN))
  1312. return -EPERM;
  1313. spin_lock_irqsave(&wl->lock, irqflag);
  1314. p = extra;
  1315. if (test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat)) {
  1316. if (wl->psk_type == GELIC_EURUS_WPA_PSK_BIN) {
  1317. for (i = 0; i < wl->psk_len; i++) {
  1318. sprintf(p, "%02xu", wl->psk[i]);
  1319. p += 2;
  1320. }
  1321. *p = '\0';
  1322. data->data.length = wl->psk_len * 2;
  1323. } else {
  1324. *p++ = '"';
  1325. memcpy(p, wl->psk, wl->psk_len);
  1326. p += wl->psk_len;
  1327. *p++ = '"';
  1328. *p = '\0';
  1329. data->data.length = wl->psk_len + 2;
  1330. }
  1331. } else
  1332. /* no psk set */
  1333. data->data.length = 0;
  1334. spin_unlock_irqrestore(&wl->lock, irqflag);
  1335. pr_debug("%s:-> %d\n", __func__, data->data.length);
  1336. return 0;
  1337. }
  1338. /* SIOCGIWNICKN */
  1339. static int gelic_wl_get_nick(struct net_device *net_dev,
  1340. struct iw_request_info *info,
  1341. union iwreq_data *data, char *extra)
  1342. {
  1343. strcpy(extra, "gelic_wl");
  1344. data->data.length = strlen(extra);
  1345. data->data.flags = 1;
  1346. return 0;
  1347. }
  1348. /* --- */
  1349. static struct iw_statistics *gelic_wl_get_wireless_stats(
  1350. struct net_device *netdev)
  1351. {
  1352. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  1353. struct gelic_eurus_cmd *cmd;
  1354. struct iw_statistics *is;
  1355. struct gelic_eurus_rssi_info *rssi;
  1356. pr_debug("%s: <-\n", __func__);
  1357. is = &wl->iwstat;
  1358. memset(is, 0, sizeof(*is));
  1359. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
  1360. wl->buf, sizeof(*rssi));
  1361. if (cmd && !cmd->status && !cmd->cmd_status) {
  1362. rssi = wl->buf;
  1363. is->qual.level = be16_to_cpu(rssi->rssi);
  1364. is->qual.updated = IW_QUAL_LEVEL_UPDATED |
  1365. IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
  1366. } else
  1367. /* not associated */
  1368. is->qual.updated = IW_QUAL_ALL_INVALID;
  1369. kfree(cmd);
  1370. pr_debug("%s: ->\n", __func__);
  1371. return is;
  1372. }
  1373. /*
  1374. * scanning helpers
  1375. */
  1376. static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan)
  1377. {
  1378. struct gelic_eurus_cmd *cmd;
  1379. int ret = 0;
  1380. pr_debug("%s: <- always=%d\n", __func__, always_scan);
  1381. if (mutex_lock_interruptible(&wl->scan_lock))
  1382. return -ERESTARTSYS;
  1383. /*
  1384. * If already a scan in progress, do not trigger more
  1385. */
  1386. if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
  1387. pr_debug("%s: scanning now\n", __func__);
  1388. goto out;
  1389. }
  1390. init_completion(&wl->scan_done);
  1391. /*
  1392. * If we have already a bss list, don't try to get new
  1393. */
  1394. if (!always_scan && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
  1395. pr_debug("%s: already has the list\n", __func__);
  1396. complete(&wl->scan_done);
  1397. goto out;
  1398. }
  1399. /*
  1400. * issue start scan request
  1401. */
  1402. wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
  1403. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
  1404. NULL, 0);
  1405. if (!cmd || cmd->status || cmd->cmd_status) {
  1406. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  1407. complete(&wl->scan_done);
  1408. ret = -ENOMEM;
  1409. goto out;
  1410. }
  1411. kfree(cmd);
  1412. out:
  1413. mutex_unlock(&wl->scan_lock);
  1414. pr_debug("%s: ->\n", __func__);
  1415. return ret;
  1416. }
  1417. /*
  1418. * retrieve scan result from the chip (hypervisor)
  1419. * this function is invoked by schedule work.
  1420. */
  1421. static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
  1422. {
  1423. struct gelic_eurus_cmd *cmd = NULL;
  1424. struct gelic_wl_scan_info *target, *tmp;
  1425. struct gelic_wl_scan_info *oldest = NULL;
  1426. struct gelic_eurus_scan_info *scan_info;
  1427. unsigned int scan_info_size;
  1428. union iwreq_data data;
  1429. unsigned long this_time = jiffies;
  1430. unsigned int data_len, i, found, r;
  1431. DECLARE_MAC_BUF(mac);
  1432. pr_debug("%s:start\n", __func__);
  1433. mutex_lock(&wl->scan_lock);
  1434. if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
  1435. /*
  1436. * stop() may be called while scanning, ignore result
  1437. */
  1438. pr_debug("%s: scan complete when stat != scanning(%d)\n",
  1439. __func__, wl->scan_stat);
  1440. goto out;
  1441. }
  1442. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
  1443. wl->buf, PAGE_SIZE);
  1444. if (!cmd || cmd->status || cmd->cmd_status) {
  1445. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  1446. pr_info("%s:cmd failed\n", __func__);
  1447. kfree(cmd);
  1448. goto out;
  1449. }
  1450. data_len = cmd->size;
  1451. pr_debug("%s: data_len = %d\n", __func__, data_len);
  1452. kfree(cmd);
  1453. /* OK, bss list retrieved */
  1454. wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;
  1455. /* mark all entries are old */
  1456. list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
  1457. target->valid = 0;
  1458. /* expire too old entries */
  1459. if (time_before(target->last_scanned + wl->scan_age,
  1460. this_time)) {
  1461. kfree(target->hwinfo);
  1462. target->hwinfo = NULL;
  1463. list_move_tail(&target->list, &wl->network_free_list);
  1464. }
  1465. }
  1466. /* put them in the newtork_list */
  1467. for (i = 0, scan_info_size = 0, scan_info = wl->buf;
  1468. scan_info_size < data_len;
  1469. i++, scan_info_size += be16_to_cpu(scan_info->size),
  1470. scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
  1471. pr_debug("%s:size=%d bssid=%s scan_info=%p\n", __func__,
  1472. be16_to_cpu(scan_info->size),
  1473. print_mac(mac, &scan_info->bssid[2]), scan_info);
  1474. /*
  1475. * The wireless firmware may return invalid channel 0 and/or
  1476. * invalid rate if the AP emits zero length SSID ie. As this
  1477. * scan information is useless, ignore it
  1478. */
  1479. if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
  1480. pr_debug("%s: invalid scan info\n", __func__);
  1481. continue;
  1482. }
  1483. found = 0;
  1484. oldest = NULL;
  1485. list_for_each_entry(target, &wl->network_list, list) {
  1486. if (!compare_ether_addr(&target->hwinfo->bssid[2],
  1487. &scan_info->bssid[2])) {
  1488. found = 1;
  1489. pr_debug("%s: same BBS found scanned list\n",
  1490. __func__);
  1491. break;
  1492. }
  1493. if (!oldest ||
  1494. (target->last_scanned < oldest->last_scanned))
  1495. oldest = target;
  1496. }
  1497. if (!found) {
  1498. /* not found in the list */
  1499. if (list_empty(&wl->network_free_list)) {
  1500. /* expire oldest */
  1501. target = oldest;
  1502. } else {
  1503. target = list_entry(wl->network_free_list.next,
  1504. struct gelic_wl_scan_info,
  1505. list);
  1506. }
  1507. }
  1508. /* update the item */
  1509. target->last_scanned = this_time;
  1510. target->valid = 1;
  1511. target->eurus_index = i;
  1512. kfree(target->hwinfo);
  1513. target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
  1514. GFP_KERNEL);
  1515. if (!target->hwinfo) {
  1516. pr_info("%s: kzalloc failed\n", __func__);
  1517. continue;
  1518. }
  1519. /* copy hw scan info */
  1520. memcpy(target->hwinfo, scan_info, scan_info->size);
  1521. target->essid_len = strnlen(scan_info->essid,
  1522. sizeof(scan_info->essid));
  1523. target->rate_len = 0;
  1524. for (r = 0; r < MAX_RATES_LENGTH; r++)
  1525. if (scan_info->rate[r])
  1526. target->rate_len++;
  1527. if (8 < target->rate_len)
  1528. pr_info("%s: AP returns %d rates\n", __func__,
  1529. target->rate_len);
  1530. target->rate_ext_len = 0;
  1531. for (r = 0; r < MAX_RATES_EX_LENGTH; r++)
  1532. if (scan_info->ext_rate[r])
  1533. target->rate_ext_len++;
  1534. list_move_tail(&target->list, &wl->network_list);
  1535. }
  1536. memset(&data, 0, sizeof(data));
  1537. wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
  1538. NULL);
  1539. out:
  1540. complete(&wl->scan_done);
  1541. mutex_unlock(&wl->scan_lock);
  1542. pr_debug("%s:end\n", __func__);
  1543. }
  1544. /*
  1545. * Select an appropriate bss from current scan list regarding
  1546. * current settings from userspace.
  1547. * The caller must hold wl->scan_lock,
  1548. * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
  1549. */
  1550. static void update_best(struct gelic_wl_scan_info **best,
  1551. struct gelic_wl_scan_info *candid,
  1552. int *best_weight,
  1553. int *weight)
  1554. {
  1555. if (*best_weight < ++(*weight)) {
  1556. *best_weight = *weight;
  1557. *best = candid;
  1558. }
  1559. }
  1560. static
  1561. struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
  1562. {
  1563. struct gelic_wl_scan_info *scan_info;
  1564. struct gelic_wl_scan_info *best_bss;
  1565. int weight, best_weight;
  1566. u16 security;
  1567. DECLARE_MAC_BUF(mac);
  1568. pr_debug("%s: <-\n", __func__);
  1569. best_bss = NULL;
  1570. best_weight = 0;
  1571. list_for_each_entry(scan_info, &wl->network_list, list) {
  1572. pr_debug("%s: station %p\n", __func__, scan_info);
  1573. if (!scan_info->valid) {
  1574. pr_debug("%s: station invalid\n", __func__);
  1575. continue;
  1576. }
  1577. /* If bss specified, check it only */
  1578. if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
  1579. if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
  1580. wl->bssid)) {
  1581. best_bss = scan_info;
  1582. pr_debug("%s: bssid matched\n", __func__);
  1583. break;
  1584. } else {
  1585. pr_debug("%s: bssid unmached\n", __func__);
  1586. continue;
  1587. }
  1588. }
  1589. weight = 0;
  1590. /* security */
  1591. security = be16_to_cpu(scan_info->hwinfo->security) &
  1592. GELIC_EURUS_SCAN_SEC_MASK;
  1593. if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
  1594. if (security == GELIC_EURUS_SCAN_SEC_WPA2)
  1595. update_best(&best_bss, scan_info,
  1596. &best_weight, &weight);
  1597. else
  1598. continue;
  1599. } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
  1600. if (security == GELIC_EURUS_SCAN_SEC_WPA)
  1601. update_best(&best_bss, scan_info,
  1602. &best_weight, &weight);
  1603. else
  1604. continue;
  1605. } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
  1606. wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  1607. if (security == GELIC_EURUS_SCAN_SEC_WEP)
  1608. update_best(&best_bss, scan_info,
  1609. &best_weight, &weight);
  1610. else
  1611. continue;
  1612. }
  1613. /* If ESSID is set, check it */
  1614. if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
  1615. if ((scan_info->essid_len == wl->essid_len) &&
  1616. !strncmp(wl->essid,
  1617. scan_info->hwinfo->essid,
  1618. scan_info->essid_len))
  1619. update_best(&best_bss, scan_info,
  1620. &best_weight, &weight);
  1621. else
  1622. continue;
  1623. }
  1624. }
  1625. #ifdef DEBUG
  1626. pr_debug("%s: -> bss=%p\n", __func__, best_bss);
  1627. if (best_bss) {
  1628. pr_debug("%s:addr=%s\n", __func__,
  1629. print_mac(mac, &best_bss->hwinfo->bssid[2]));
  1630. }
  1631. #endif
  1632. return best_bss;
  1633. }
  1634. /*
  1635. * Setup WEP configuration to the chip
  1636. * The caller must hold wl->scan_lock,
  1637. * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
  1638. */
  1639. static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
  1640. {
  1641. unsigned int i;
  1642. struct gelic_eurus_wep_cfg *wep;
  1643. struct gelic_eurus_cmd *cmd;
  1644. int wep104 = 0;
  1645. int have_key = 0;
  1646. int ret = 0;
  1647. pr_debug("%s: <-\n", __func__);
  1648. /* we can assume no one should uses the buffer */
  1649. wep = wl->buf;
  1650. memset(wep, 0, sizeof(*wep));
  1651. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  1652. pr_debug("%s: WEP mode\n", __func__);
  1653. for (i = 0; i < GELIC_WEP_KEYS; i++) {
  1654. if (!test_bit(i, &wl->key_enabled))
  1655. continue;
  1656. pr_debug("%s: key#%d enabled\n", __func__, i);
  1657. have_key = 1;
  1658. if (wl->key_len[i] == 13)
  1659. wep104 = 1;
  1660. else if (wl->key_len[i] != 5) {
  1661. pr_info("%s: wrong wep key[%d]=%d\n",
  1662. __func__, i, wl->key_len[i]);
  1663. ret = -EINVAL;
  1664. goto out;
  1665. }
  1666. memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
  1667. }
  1668. if (!have_key) {
  1669. pr_info("%s: all wep key disabled\n", __func__);
  1670. ret = -EINVAL;
  1671. goto out;
  1672. }
  1673. if (wep104) {
  1674. pr_debug("%s: 104bit key\n", __func__);
  1675. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
  1676. } else {
  1677. pr_debug("%s: 40bit key\n", __func__);
  1678. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
  1679. }
  1680. } else {
  1681. pr_debug("%s: NO encryption\n", __func__);
  1682. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
  1683. }
  1684. /* issue wep setup */
  1685. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
  1686. wep, sizeof(*wep));
  1687. if (!cmd)
  1688. ret = -ENOMEM;
  1689. else if (cmd->status || cmd->cmd_status)
  1690. ret = -ENXIO;
  1691. kfree(cmd);
  1692. out:
  1693. pr_debug("%s: ->\n", __func__);
  1694. return ret;
  1695. }
  1696. #ifdef DEBUG
  1697. static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
  1698. {
  1699. switch (sec) {
  1700. case GELIC_EURUS_WPA_SEC_NONE:
  1701. return "NONE";
  1702. break;
  1703. case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
  1704. return "WPA_TKIP_TKIP";
  1705. break;
  1706. case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
  1707. return "WPA_TKIP_AES";
  1708. break;
  1709. case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
  1710. return "WPA_AES_AES";
  1711. break;
  1712. case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
  1713. return "WPA2_TKIP_TKIP";
  1714. break;
  1715. case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
  1716. return "WPA2_TKIP_AES";
  1717. break;
  1718. case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
  1719. return "WPA2_AES_AES";
  1720. break;
  1721. }
  1722. return "";
  1723. };
  1724. #endif
  1725. static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
  1726. {
  1727. struct gelic_eurus_wpa_cfg *wpa;
  1728. struct gelic_eurus_cmd *cmd;
  1729. u16 security;
  1730. int ret = 0;
  1731. pr_debug("%s: <-\n", __func__);
  1732. /* we can assume no one should uses the buffer */
  1733. wpa = wl->buf;
  1734. memset(wpa, 0, sizeof(*wpa));
  1735. if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
  1736. pr_info("%s: PSK not configured yet\n", __func__);
  1737. /* copy key */
  1738. memcpy(wpa->psk, wl->psk, wl->psk_len);
  1739. /* set security level */
  1740. if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
  1741. if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
  1742. security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
  1743. } else {
  1744. if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
  1745. precise_ie())
  1746. security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
  1747. else
  1748. security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
  1749. }
  1750. } else {
  1751. if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
  1752. security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
  1753. } else {
  1754. if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
  1755. precise_ie())
  1756. security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
  1757. else
  1758. security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
  1759. }
  1760. }
  1761. wpa->security = cpu_to_be16(security);
  1762. /* PSK type */
  1763. wpa->psk_type = cpu_to_be16(wl->psk_type);
  1764. #ifdef DEBUG
  1765. pr_debug("%s: sec=%s psktype=%s\nn", __func__,
  1766. wpasecstr(wpa->security),
  1767. (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
  1768. "BIN" : "passphrase");
  1769. #if 0
  1770. /*
  1771. * don't enable here if you plan to submit
  1772. * the debug log because this dumps your precious
  1773. * passphrase/key.
  1774. */
  1775. pr_debug("%s: psk=%s\n",
  1776. (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
  1777. (char *)"N/A" : (char *)wpa->psk);
  1778. #endif
  1779. #endif
  1780. /* issue wpa setup */
  1781. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
  1782. wpa, sizeof(*wpa));
  1783. if (!cmd)
  1784. ret = -ENOMEM;
  1785. else if (cmd->status || cmd->cmd_status)
  1786. ret = -ENXIO;
  1787. kfree(cmd);
  1788. pr_debug("%s: --> %d\n", __func__, ret);
  1789. return ret;
  1790. }
  1791. /*
  1792. * Start association. caller must hold assoc_stat_lock
  1793. */
  1794. static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
  1795. struct gelic_wl_scan_info *bss)
  1796. {
  1797. struct gelic_eurus_cmd *cmd;
  1798. struct gelic_eurus_common_cfg *common;
  1799. int ret = 0;
  1800. unsigned long rc;
  1801. pr_debug("%s: <-\n", __func__);
  1802. /* do common config */
  1803. common = wl->buf;
  1804. memset(common, 0, sizeof(*common));
  1805. common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
  1806. common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
  1807. common->scan_index = cpu_to_be16(bss->eurus_index);
  1808. switch (wl->auth_method) {
  1809. case GELIC_EURUS_AUTH_OPEN:
  1810. common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
  1811. break;
  1812. case GELIC_EURUS_AUTH_SHARED:
  1813. common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
  1814. break;
  1815. }
  1816. #ifdef DEBUG
  1817. scan_list_dump(wl);
  1818. #endif
  1819. pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
  1820. be16_to_cpu(common->scan_index),
  1821. be16_to_cpu(common->bss_type),
  1822. be16_to_cpu(common->auth_method));
  1823. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
  1824. common, sizeof(*common));
  1825. if (!cmd || cmd->status || cmd->cmd_status) {
  1826. ret = -ENOMEM;
  1827. kfree(cmd);
  1828. goto out;
  1829. }
  1830. kfree(cmd);
  1831. /* WEP/WPA */
  1832. switch (wl->wpa_level) {
  1833. case GELIC_WL_WPA_LEVEL_NONE:
  1834. /* If WEP or no security, setup WEP config */
  1835. ret = gelic_wl_do_wep_setup(wl);
  1836. break;
  1837. case GELIC_WL_WPA_LEVEL_WPA:
  1838. case GELIC_WL_WPA_LEVEL_WPA2:
  1839. ret = gelic_wl_do_wpa_setup(wl);
  1840. break;
  1841. };
  1842. if (ret) {
  1843. pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
  1844. ret);
  1845. }
  1846. /* start association */
  1847. init_completion(&wl->assoc_done);
  1848. wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
  1849. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
  1850. NULL, 0);
  1851. if (!cmd || cmd->status || cmd->cmd_status) {
  1852. pr_debug("%s: assoc request failed\n", __func__);
  1853. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1854. kfree(cmd);
  1855. ret = -ENOMEM;
  1856. gelic_wl_send_iwap_event(wl, NULL);
  1857. goto out;
  1858. }
  1859. kfree(cmd);
  1860. /* wait for connected event */
  1861. rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
  1862. if (!rc) {
  1863. /* timeouted. Maybe key or cyrpt mode is wrong */
  1864. pr_info("%s: connect timeout \n", __func__);
  1865. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
  1866. NULL, 0);
  1867. kfree(cmd);
  1868. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1869. gelic_wl_send_iwap_event(wl, NULL);
  1870. ret = -ENXIO;
  1871. } else {
  1872. wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
  1873. /* copy bssid */
  1874. memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
  1875. /* send connect event */
  1876. gelic_wl_send_iwap_event(wl, wl->active_bssid);
  1877. pr_info("%s: connected\n", __func__);
  1878. }
  1879. out:
  1880. pr_debug("%s: ->\n", __func__);
  1881. return ret;
  1882. }
  1883. /*
  1884. * connected event
  1885. */
  1886. static void gelic_wl_connected_event(struct gelic_wl_info *wl,
  1887. u64 event)
  1888. {
  1889. u64 desired_event = 0;
  1890. switch (wl->wpa_level) {
  1891. case GELIC_WL_WPA_LEVEL_NONE:
  1892. desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
  1893. break;
  1894. case GELIC_WL_WPA_LEVEL_WPA:
  1895. case GELIC_WL_WPA_LEVEL_WPA2:
  1896. desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
  1897. break;
  1898. }
  1899. if (desired_event == event) {
  1900. pr_debug("%s: completed \n", __func__);
  1901. complete(&wl->assoc_done);
  1902. netif_carrier_on(port_to_netdev(wl_port(wl)));
  1903. } else
  1904. pr_debug("%s: event %#lx under wpa\n",
  1905. __func__, event);
  1906. }
  1907. /*
  1908. * disconnect event
  1909. */
  1910. static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
  1911. u64 event)
  1912. {
  1913. struct gelic_eurus_cmd *cmd;
  1914. int lock;
  1915. /*
  1916. * If we fall here in the middle of association,
  1917. * associate_bss() should be waiting for complation of
  1918. * wl->assoc_done.
  1919. * As it waits with timeout, just leave assoc_done
  1920. * uncompleted, then it terminates with timeout
  1921. */
  1922. if (!mutex_trylock(&wl->assoc_stat_lock)) {
  1923. pr_debug("%s: already locked\n", __func__);
  1924. lock = 0;
  1925. } else {
  1926. pr_debug("%s: obtain lock\n", __func__);
  1927. lock = 1;
  1928. }
  1929. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
  1930. kfree(cmd);
  1931. /* send disconnected event to the supplicant */
  1932. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  1933. gelic_wl_send_iwap_event(wl, NULL);
  1934. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1935. netif_carrier_off(port_to_netdev(wl_port(wl)));
  1936. if (lock)
  1937. mutex_unlock(&wl->assoc_stat_lock);
  1938. }
  1939. /*
  1940. * event worker
  1941. */
  1942. #ifdef DEBUG
  1943. static const char *eventstr(enum gelic_lv1_wl_event event)
  1944. {
  1945. static char buf[32];
  1946. char *ret;
  1947. if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
  1948. ret = "EURUS_READY";
  1949. else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
  1950. ret = "SCAN_COMPLETED";
  1951. else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
  1952. ret = "DEAUTH";
  1953. else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
  1954. ret = "BEACON_LOST";
  1955. else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
  1956. ret = "CONNECTED";
  1957. else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
  1958. ret = "WPA_CONNECTED";
  1959. else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
  1960. ret = "WPA_ERROR";
  1961. else {
  1962. sprintf(buf, "Unknown(%#x)", event);
  1963. ret = buf;
  1964. }
  1965. return ret;
  1966. }
  1967. #else
  1968. static const char *eventstr(enum gelic_lv1_wl_event event)
  1969. {
  1970. return NULL;
  1971. }
  1972. #endif
  1973. static void gelic_wl_event_worker(struct work_struct *work)
  1974. {
  1975. struct gelic_wl_info *wl;
  1976. struct gelic_port *port;
  1977. u64 event, tmp;
  1978. int status;
  1979. pr_debug("%s:start\n", __func__);
  1980. wl = container_of(work, struct gelic_wl_info, event_work.work);
  1981. port = wl_port(wl);
  1982. while (1) {
  1983. status = lv1_net_control(bus_id(port->card), dev_id(port->card),
  1984. GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
  1985. &event, &tmp);
  1986. if (status) {
  1987. if (status != LV1_NO_ENTRY)
  1988. pr_debug("%s:wlan event failed %d\n",
  1989. __func__, status);
  1990. /* got all events */
  1991. pr_debug("%s:end\n", __func__);
  1992. return;
  1993. }
  1994. pr_debug("%s: event=%s\n", __func__, eventstr(event));
  1995. switch (event) {
  1996. case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
  1997. gelic_wl_scan_complete_event(wl);
  1998. break;
  1999. case GELIC_LV1_WL_EVENT_BEACON_LOST:
  2000. case GELIC_LV1_WL_EVENT_DEAUTH:
  2001. gelic_wl_disconnect_event(wl, event);
  2002. break;
  2003. case GELIC_LV1_WL_EVENT_CONNECTED:
  2004. case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
  2005. gelic_wl_connected_event(wl, event);
  2006. break;
  2007. default:
  2008. break;
  2009. }
  2010. } /* while */
  2011. }
  2012. /*
  2013. * association worker
  2014. */
  2015. static void gelic_wl_assoc_worker(struct work_struct *work)
  2016. {
  2017. struct gelic_wl_info *wl;
  2018. struct gelic_wl_scan_info *best_bss;
  2019. int ret;
  2020. wl = container_of(work, struct gelic_wl_info, assoc_work.work);
  2021. mutex_lock(&wl->assoc_stat_lock);
  2022. if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
  2023. goto out;
  2024. ret = gelic_wl_start_scan(wl, 0);
  2025. if (ret == -ERESTARTSYS) {
  2026. pr_debug("%s: scan start failed association\n", __func__);
  2027. schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
  2028. goto out;
  2029. } else if (ret) {
  2030. pr_info("%s: scan prerequisite failed\n", __func__);
  2031. goto out;
  2032. }
  2033. /*
  2034. * Wait for bss scan completion
  2035. * If we have scan list already, gelic_wl_start_scan()
  2036. * returns OK and raises the complete. Thus,
  2037. * it's ok to wait unconditionally here
  2038. */
  2039. wait_for_completion(&wl->scan_done);
  2040. pr_debug("%s: scan done\n", __func__);
  2041. mutex_lock(&wl->scan_lock);
  2042. if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
  2043. gelic_wl_send_iwap_event(wl, NULL);
  2044. pr_info("%s: no scan list. association failed\n", __func__);
  2045. goto scan_lock_out;
  2046. }
  2047. /* find best matching bss */
  2048. best_bss = gelic_wl_find_best_bss(wl);
  2049. if (!best_bss) {
  2050. gelic_wl_send_iwap_event(wl, NULL);
  2051. pr_info("%s: no bss matched. association failed\n", __func__);
  2052. goto scan_lock_out;
  2053. }
  2054. /* ok, do association */
  2055. ret = gelic_wl_associate_bss(wl, best_bss);
  2056. if (ret)
  2057. pr_info("%s: association failed %d\n", __func__, ret);
  2058. scan_lock_out:
  2059. mutex_unlock(&wl->scan_lock);
  2060. out:
  2061. mutex_unlock(&wl->assoc_stat_lock);
  2062. }
  2063. /*
  2064. * Interrupt handler
  2065. * Called from the ethernet interrupt handler
  2066. * Processes wireless specific virtual interrupts only
  2067. */
  2068. void gelic_wl_interrupt(struct net_device *netdev, u64 status)
  2069. {
  2070. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  2071. if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
  2072. pr_debug("%s:cmd complete\n", __func__);
  2073. complete(&wl->cmd_done_intr);
  2074. }
  2075. if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
  2076. pr_debug("%s:event received\n", __func__);
  2077. queue_delayed_work(wl->event_queue, &wl->event_work, 0);
  2078. }
  2079. }
  2080. /*
  2081. * driver helpers
  2082. */
  2083. #define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
  2084. static const iw_handler gelic_wl_wext_handler[] =
  2085. {
  2086. IW_IOCTL(SIOCGIWNAME) = gelic_wl_get_name,
  2087. IW_IOCTL(SIOCGIWRANGE) = gelic_wl_get_range,
  2088. IW_IOCTL(SIOCSIWSCAN) = gelic_wl_set_scan,
  2089. IW_IOCTL(SIOCGIWSCAN) = gelic_wl_get_scan,
  2090. IW_IOCTL(SIOCSIWAUTH) = gelic_wl_set_auth,
  2091. IW_IOCTL(SIOCGIWAUTH) = gelic_wl_get_auth,
  2092. IW_IOCTL(SIOCSIWESSID) = gelic_wl_set_essid,
  2093. IW_IOCTL(SIOCGIWESSID) = gelic_wl_get_essid,
  2094. IW_IOCTL(SIOCSIWENCODE) = gelic_wl_set_encode,
  2095. IW_IOCTL(SIOCGIWENCODE) = gelic_wl_get_encode,
  2096. IW_IOCTL(SIOCSIWAP) = gelic_wl_set_ap,
  2097. IW_IOCTL(SIOCGIWAP) = gelic_wl_get_ap,
  2098. IW_IOCTL(SIOCSIWENCODEEXT) = gelic_wl_set_encodeext,
  2099. IW_IOCTL(SIOCGIWENCODEEXT) = gelic_wl_get_encodeext,
  2100. IW_IOCTL(SIOCSIWMODE) = gelic_wl_set_mode,
  2101. IW_IOCTL(SIOCGIWMODE) = gelic_wl_get_mode,
  2102. IW_IOCTL(SIOCGIWNICKN) = gelic_wl_get_nick,
  2103. };
  2104. static struct iw_priv_args gelic_wl_private_args[] =
  2105. {
  2106. {
  2107. .cmd = GELIC_WL_PRIV_SET_PSK,
  2108. .set_args = IW_PRIV_TYPE_CHAR |
  2109. (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
  2110. .name = "set_psk"
  2111. },
  2112. {
  2113. .cmd = GELIC_WL_PRIV_GET_PSK,
  2114. .get_args = IW_PRIV_TYPE_CHAR |
  2115. (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
  2116. .name = "get_psk"
  2117. }
  2118. };
  2119. static const iw_handler gelic_wl_private_handler[] =
  2120. {
  2121. gelic_wl_priv_set_psk,
  2122. gelic_wl_priv_get_psk,
  2123. };
  2124. static const struct iw_handler_def gelic_wl_wext_handler_def = {
  2125. .num_standard = ARRAY_SIZE(gelic_wl_wext_handler),
  2126. .standard = gelic_wl_wext_handler,
  2127. .get_wireless_stats = gelic_wl_get_wireless_stats,
  2128. .num_private = ARRAY_SIZE(gelic_wl_private_handler),
  2129. .num_private_args = ARRAY_SIZE(gelic_wl_private_args),
  2130. .private = gelic_wl_private_handler,
  2131. .private_args = gelic_wl_private_args,
  2132. };
  2133. static struct net_device *gelic_wl_alloc(struct gelic_card *card)
  2134. {
  2135. struct net_device *netdev;
  2136. struct gelic_port *port;
  2137. struct gelic_wl_info *wl;
  2138. unsigned int i;
  2139. pr_debug("%s:start\n", __func__);
  2140. netdev = alloc_etherdev(sizeof(struct gelic_port) +
  2141. sizeof(struct gelic_wl_info));
  2142. pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
  2143. if (!netdev)
  2144. return NULL;
  2145. strcpy(netdev->name, "wlan%d");
  2146. port = netdev_priv(netdev);
  2147. port->netdev = netdev;
  2148. port->card = card;
  2149. port->type = GELIC_PORT_WIRELESS;
  2150. wl = port_wl(port);
  2151. pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
  2152. /* allocate scan list */
  2153. wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
  2154. GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
  2155. if (!wl->networks)
  2156. goto fail_bss;
  2157. wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
  2158. if (!wl->eurus_cmd_queue)
  2159. goto fail_cmd_workqueue;
  2160. wl->event_queue = create_singlethread_workqueue("gelic_event");
  2161. if (!wl->event_queue)
  2162. goto fail_event_workqueue;
  2163. INIT_LIST_HEAD(&wl->network_free_list);
  2164. INIT_LIST_HEAD(&wl->network_list);
  2165. for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
  2166. list_add_tail(&wl->networks[i].list,
  2167. &wl->network_free_list);
  2168. init_completion(&wl->cmd_done_intr);
  2169. INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
  2170. INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
  2171. mutex_init(&wl->scan_lock);
  2172. mutex_init(&wl->assoc_stat_lock);
  2173. init_completion(&wl->scan_done);
  2174. /* for the case that no scan request is issued and stop() is called */
  2175. complete(&wl->scan_done);
  2176. spin_lock_init(&wl->lock);
  2177. wl->scan_age = 5*HZ; /* FIXME */
  2178. /* buffer for receiving scanned list etc */
  2179. BUILD_BUG_ON(PAGE_SIZE <
  2180. sizeof(struct gelic_eurus_scan_info) *
  2181. GELIC_EURUS_MAX_SCAN);
  2182. wl->buf = (void *)get_zeroed_page(GFP_KERNEL);
  2183. if (!wl->buf) {
  2184. pr_info("%s:buffer allocation failed\n", __func__);
  2185. goto fail_getpage;
  2186. }
  2187. pr_debug("%s:end\n", __func__);
  2188. return netdev;
  2189. fail_getpage:
  2190. destroy_workqueue(wl->event_queue);
  2191. fail_event_workqueue:
  2192. destroy_workqueue(wl->eurus_cmd_queue);
  2193. fail_cmd_workqueue:
  2194. kfree(wl->networks);
  2195. fail_bss:
  2196. free_netdev(netdev);
  2197. pr_debug("%s:end error\n", __func__);
  2198. return NULL;
  2199. }
  2200. static void gelic_wl_free(struct gelic_wl_info *wl)
  2201. {
  2202. struct gelic_wl_scan_info *scan_info;
  2203. unsigned int i;
  2204. pr_debug("%s: <-\n", __func__);
  2205. free_page((unsigned long)wl->buf);
  2206. pr_debug("%s: destroy queues\n", __func__);
  2207. destroy_workqueue(wl->eurus_cmd_queue);
  2208. destroy_workqueue(wl->event_queue);
  2209. scan_info = wl->networks;
  2210. for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
  2211. kfree(scan_info->hwinfo);
  2212. kfree(wl->networks);
  2213. free_netdev(port_to_netdev(wl_port(wl)));
  2214. pr_debug("%s: ->\n", __func__);
  2215. }
  2216. static int gelic_wl_try_associate(struct net_device *netdev)
  2217. {
  2218. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  2219. int ret = -1;
  2220. unsigned int i;
  2221. pr_debug("%s: <-\n", __func__);
  2222. /* check constraits for start association */
  2223. /* for no access restriction AP */
  2224. if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
  2225. if (test_bit(GELIC_WL_STAT_CONFIGURED,
  2226. &wl->stat))
  2227. goto do_associate;
  2228. else {
  2229. pr_debug("%s: no wep, not configured\n", __func__);
  2230. return ret;
  2231. }
  2232. }
  2233. /* for WEP, one of four keys should be set */
  2234. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  2235. /* one of keys set */
  2236. for (i = 0; i < GELIC_WEP_KEYS; i++) {
  2237. if (test_bit(i, &wl->key_enabled))
  2238. goto do_associate;
  2239. }
  2240. pr_debug("%s: WEP, but no key specified\n", __func__);
  2241. return ret;
  2242. }
  2243. /* for WPA[2], psk should be set */
  2244. if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
  2245. (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
  2246. if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
  2247. &wl->stat))
  2248. goto do_associate;
  2249. else {
  2250. pr_debug("%s: AES/TKIP, but PSK not configured\n",
  2251. __func__);
  2252. return ret;
  2253. }
  2254. }
  2255. do_associate:
  2256. ret = schedule_delayed_work(&wl->assoc_work, 0);
  2257. pr_debug("%s: start association work %d\n", __func__, ret);
  2258. return ret;
  2259. }
  2260. /*
  2261. * netdev handlers
  2262. */
  2263. static int gelic_wl_open(struct net_device *netdev)
  2264. {
  2265. struct gelic_card *card = netdev_card(netdev);
  2266. pr_debug("%s:->%p\n", __func__, netdev);
  2267. gelic_card_up(card);
  2268. /* try to associate */
  2269. gelic_wl_try_associate(netdev);
  2270. netif_start_queue(netdev);
  2271. pr_debug("%s:<-\n", __func__);
  2272. return 0;
  2273. }
  2274. /*
  2275. * reset state machine
  2276. */
  2277. static int gelic_wl_reset_state(struct gelic_wl_info *wl)
  2278. {
  2279. struct gelic_wl_scan_info *target;
  2280. struct gelic_wl_scan_info *tmp;
  2281. /* empty scan list */
  2282. list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
  2283. list_move_tail(&target->list, &wl->network_free_list);
  2284. }
  2285. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  2286. /* clear configuration */
  2287. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  2288. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  2289. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  2290. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  2291. wl->key_enabled = 0;
  2292. wl->current_key = 0;
  2293. wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
  2294. wl->psk_len = 0;
  2295. wl->essid_len = 0;
  2296. memset(wl->essid, 0, sizeof(wl->essid));
  2297. memset(wl->bssid, 0, sizeof(wl->bssid));
  2298. memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
  2299. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  2300. memset(&wl->iwstat, 0, sizeof(wl->iwstat));
  2301. /* all status bit clear */
  2302. wl->stat = 0;
  2303. return 0;
  2304. }
  2305. /*
  2306. * Tell eurus to terminate association
  2307. */
  2308. static void gelic_wl_disconnect(struct net_device *netdev)
  2309. {
  2310. struct gelic_port *port = netdev_priv(netdev);
  2311. struct gelic_wl_info *wl = port_wl(port);
  2312. struct gelic_eurus_cmd *cmd;
  2313. /*
  2314. * If scann process is running on chip,
  2315. * further requests will be rejected
  2316. */
  2317. if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
  2318. wait_for_completion_timeout(&wl->scan_done, HZ);
  2319. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
  2320. kfree(cmd);
  2321. gelic_wl_send_iwap_event(wl, NULL);
  2322. };
  2323. static int gelic_wl_stop(struct net_device *netdev)
  2324. {
  2325. struct gelic_port *port = netdev_priv(netdev);
  2326. struct gelic_wl_info *wl = port_wl(port);
  2327. struct gelic_card *card = netdev_card(netdev);
  2328. pr_debug("%s:<-\n", __func__);
  2329. /*
  2330. * Cancel pending association work.
  2331. * event work can run after netdev down
  2332. */
  2333. cancel_delayed_work(&wl->assoc_work);
  2334. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  2335. gelic_wl_disconnect(netdev);
  2336. /* reset our state machine */
  2337. gelic_wl_reset_state(wl);
  2338. netif_stop_queue(netdev);
  2339. gelic_card_down(card);
  2340. pr_debug("%s:->\n", __func__);
  2341. return 0;
  2342. }
  2343. /* -- */
  2344. static struct ethtool_ops gelic_wl_ethtool_ops = {
  2345. .get_drvinfo = gelic_net_get_drvinfo,
  2346. .get_link = gelic_wl_get_link,
  2347. .get_tx_csum = ethtool_op_get_tx_csum,
  2348. .set_tx_csum = ethtool_op_set_tx_csum,
  2349. .get_rx_csum = gelic_net_get_rx_csum,
  2350. .set_rx_csum = gelic_net_set_rx_csum,
  2351. };
  2352. static void gelic_wl_setup_netdev_ops(struct net_device *netdev)
  2353. {
  2354. struct gelic_wl_info *wl;
  2355. wl = port_wl(netdev_priv(netdev));
  2356. BUG_ON(!wl);
  2357. netdev->open = &gelic_wl_open;
  2358. netdev->stop = &gelic_wl_stop;
  2359. netdev->hard_start_xmit = &gelic_net_xmit;
  2360. netdev->set_multicast_list = &gelic_net_set_multi;
  2361. netdev->change_mtu = &gelic_net_change_mtu;
  2362. netdev->wireless_data = &wl->wireless_data;
  2363. netdev->wireless_handlers = &gelic_wl_wext_handler_def;
  2364. /* tx watchdog */
  2365. netdev->tx_timeout = &gelic_net_tx_timeout;
  2366. netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
  2367. netdev->ethtool_ops = &gelic_wl_ethtool_ops;
  2368. #ifdef CONFIG_NET_POLL_CONTROLLER
  2369. netdev->poll_controller = gelic_net_poll_controller;
  2370. #endif
  2371. }
  2372. /*
  2373. * driver probe/remove
  2374. */
  2375. int gelic_wl_driver_probe(struct gelic_card *card)
  2376. {
  2377. int ret;
  2378. struct net_device *netdev;
  2379. pr_debug("%s:start\n", __func__);
  2380. if (ps3_compare_firmware_version(1, 6, 0) < 0)
  2381. return 0;
  2382. if (!card->vlan[GELIC_PORT_WIRELESS].tx)
  2383. return 0;
  2384. /* alloc netdevice for wireless */
  2385. netdev = gelic_wl_alloc(card);
  2386. if (!netdev)
  2387. return -ENOMEM;
  2388. /* setup net_device structure */
  2389. SET_NETDEV_DEV(netdev, &card->dev->core);
  2390. gelic_wl_setup_netdev_ops(netdev);
  2391. /* setup some of net_device and register it */
  2392. ret = gelic_net_setup_netdev(netdev, card);
  2393. if (ret)
  2394. goto fail_setup;
  2395. card->netdev[GELIC_PORT_WIRELESS] = netdev;
  2396. /* add enable wireless interrupt */
  2397. card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
  2398. GELIC_CARD_WLAN_COMMAND_COMPLETED;
  2399. /* to allow wireless commands while both interfaces are down */
  2400. gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
  2401. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  2402. pr_debug("%s:end\n", __func__);
  2403. return 0;
  2404. fail_setup:
  2405. gelic_wl_free(port_wl(netdev_port(netdev)));
  2406. return ret;
  2407. }
  2408. int gelic_wl_driver_remove(struct gelic_card *card)
  2409. {
  2410. struct gelic_wl_info *wl;
  2411. struct net_device *netdev;
  2412. pr_debug("%s:start\n", __func__);
  2413. if (ps3_compare_firmware_version(1, 6, 0) < 0)
  2414. return 0;
  2415. if (!card->vlan[GELIC_PORT_WIRELESS].tx)
  2416. return 0;
  2417. netdev = card->netdev[GELIC_PORT_WIRELESS];
  2418. wl = port_wl(netdev_priv(netdev));
  2419. /* if the interface was not up, but associated */
  2420. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  2421. gelic_wl_disconnect(netdev);
  2422. complete(&wl->cmd_done_intr);
  2423. /* cancel all work queue */
  2424. cancel_delayed_work(&wl->assoc_work);
  2425. cancel_delayed_work(&wl->event_work);
  2426. flush_workqueue(wl->eurus_cmd_queue);
  2427. flush_workqueue(wl->event_queue);
  2428. unregister_netdev(netdev);
  2429. /* disable wireless interrupt */
  2430. pr_debug("%s: disable intr\n", __func__);
  2431. card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
  2432. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  2433. /* free bss list, netdev*/
  2434. gelic_wl_free(wl);
  2435. pr_debug("%s:end\n", __func__);
  2436. return 0;
  2437. }