ps3_gelic_wireless.c 70 KB

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