ps3_gelic_wireless.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  1. /*
  2. * PS3 gelic network driver.
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2007 Sony Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #undef DEBUG
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/in.h>
  27. #include <linux/ip.h>
  28. #include <linux/tcp.h>
  29. #include <linux/wireless.h>
  30. #include <linux/ctype.h>
  31. #include <linux/string.h>
  32. #include <net/iw_handler.h>
  33. #include <net/ieee80211.h>
  34. #include <linux/dma-mapping.h>
  35. #include <net/checksum.h>
  36. #include <asm/firmware.h>
  37. #include <asm/ps3.h>
  38. #include <asm/lv1call.h>
  39. #include "ps3_gelic_net.h"
  40. #include "ps3_gelic_wireless.h"
  41. static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan);
  42. static int gelic_wl_try_associate(struct net_device *netdev);
  43. /*
  44. * tables
  45. */
  46. /* 802.11b/g channel to freq in MHz */
  47. static const int channel_freq[] = {
  48. 2412, 2417, 2422, 2427, 2432,
  49. 2437, 2442, 2447, 2452, 2457,
  50. 2462, 2467, 2472, 2484
  51. };
  52. #define NUM_CHANNELS ARRAY_SIZE(channel_freq)
  53. /* in bps */
  54. static const int bitrate_list[] = {
  55. 1000000,
  56. 2000000,
  57. 5500000,
  58. 11000000,
  59. 6000000,
  60. 9000000,
  61. 12000000,
  62. 18000000,
  63. 24000000,
  64. 36000000,
  65. 48000000,
  66. 54000000
  67. };
  68. #define NUM_BITRATES ARRAY_SIZE(bitrate_list)
  69. /*
  70. * wpa2 support requires the hypervisor version 2.0 or later
  71. */
  72. static inline int wpa2_capable(void)
  73. {
  74. return (0 <= ps3_compare_firmware_version(2, 0, 0));
  75. }
  76. static inline int precise_ie(void)
  77. {
  78. return (0 <= ps3_compare_firmware_version(2, 2, 0));
  79. }
  80. /*
  81. * post_eurus_cmd helpers
  82. */
  83. struct eurus_cmd_arg_info {
  84. int pre_arg; /* command requres arg1, arg2 at POST COMMAND */
  85. int post_arg; /* command requires arg1, arg2 at GET_RESULT */
  86. };
  87. static const struct eurus_cmd_arg_info cmd_info[GELIC_EURUS_CMD_MAX_INDEX] = {
  88. [GELIC_EURUS_CMD_SET_COMMON_CFG] = { .pre_arg = 1},
  89. [GELIC_EURUS_CMD_SET_WEP_CFG] = { .pre_arg = 1},
  90. [GELIC_EURUS_CMD_SET_WPA_CFG] = { .pre_arg = 1},
  91. [GELIC_EURUS_CMD_GET_COMMON_CFG] = { .post_arg = 1},
  92. [GELIC_EURUS_CMD_GET_WEP_CFG] = { .post_arg = 1},
  93. [GELIC_EURUS_CMD_GET_WPA_CFG] = { .post_arg = 1},
  94. [GELIC_EURUS_CMD_GET_RSSI_CFG] = { .post_arg = 1},
  95. [GELIC_EURUS_CMD_GET_SCAN] = { .post_arg = 1},
  96. };
  97. #ifdef DEBUG
  98. static const char *cmdstr(enum gelic_eurus_command ix)
  99. {
  100. switch (ix) {
  101. case GELIC_EURUS_CMD_ASSOC:
  102. return "ASSOC";
  103. case GELIC_EURUS_CMD_DISASSOC:
  104. return "DISASSOC";
  105. case GELIC_EURUS_CMD_START_SCAN:
  106. return "SCAN";
  107. case GELIC_EURUS_CMD_GET_SCAN:
  108. return "GET SCAN";
  109. case GELIC_EURUS_CMD_SET_COMMON_CFG:
  110. return "SET_COMMON_CFG";
  111. case GELIC_EURUS_CMD_GET_COMMON_CFG:
  112. return "GET_COMMON_CFG";
  113. case GELIC_EURUS_CMD_SET_WEP_CFG:
  114. return "SET_WEP_CFG";
  115. case GELIC_EURUS_CMD_GET_WEP_CFG:
  116. return "GET_WEP_CFG";
  117. case GELIC_EURUS_CMD_SET_WPA_CFG:
  118. return "SET_WPA_CFG";
  119. case GELIC_EURUS_CMD_GET_WPA_CFG:
  120. return "GET_WPA_CFG";
  121. case GELIC_EURUS_CMD_GET_RSSI_CFG:
  122. return "GET_RSSI";
  123. default:
  124. break;
  125. }
  126. return "";
  127. };
  128. #else
  129. static inline const char *cmdstr(enum gelic_eurus_command ix)
  130. {
  131. return "";
  132. }
  133. #endif
  134. /* synchronously do eurus commands */
  135. static void gelic_eurus_sync_cmd_worker(struct work_struct *work)
  136. {
  137. struct gelic_eurus_cmd *cmd;
  138. struct gelic_card *card;
  139. struct gelic_wl_info *wl;
  140. u64 arg1, arg2;
  141. pr_debug("%s: <-\n", __func__);
  142. cmd = container_of(work, struct gelic_eurus_cmd, work);
  143. BUG_ON(cmd_info[cmd->cmd].pre_arg &&
  144. cmd_info[cmd->cmd].post_arg);
  145. wl = cmd->wl;
  146. card = port_to_card(wl_port(wl));
  147. if (cmd_info[cmd->cmd].pre_arg) {
  148. arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
  149. arg2 = cmd->buf_size;
  150. } else {
  151. arg1 = 0;
  152. arg2 = 0;
  153. }
  154. init_completion(&wl->cmd_done_intr);
  155. pr_debug("%s: cmd='%s' start\n", __func__, cmdstr(cmd->cmd));
  156. cmd->status = lv1_net_control(bus_id(card), dev_id(card),
  157. GELIC_LV1_POST_WLAN_CMD,
  158. cmd->cmd, arg1, arg2,
  159. &cmd->tag, &cmd->size);
  160. if (cmd->status) {
  161. complete(&cmd->done);
  162. pr_info("%s: cmd issue failed\n", __func__);
  163. return;
  164. }
  165. wait_for_completion(&wl->cmd_done_intr);
  166. if (cmd_info[cmd->cmd].post_arg) {
  167. arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
  168. arg2 = cmd->buf_size;
  169. } else {
  170. arg1 = 0;
  171. arg2 = 0;
  172. }
  173. cmd->status = lv1_net_control(bus_id(card), dev_id(card),
  174. GELIC_LV1_GET_WLAN_CMD_RESULT,
  175. cmd->tag, arg1, arg2,
  176. &cmd->cmd_status, &cmd->size);
  177. #ifdef DEBUG
  178. if (cmd->status || cmd->cmd_status) {
  179. pr_debug("%s: cmd done tag=%#lx arg1=%#lx, arg2=%#lx\n", __func__,
  180. cmd->tag, arg1, arg2);
  181. pr_debug("%s: cmd done status=%#x cmd_status=%#lx size=%#lx\n",
  182. __func__, cmd->status, cmd->cmd_status, cmd->size);
  183. }
  184. #endif
  185. complete(&cmd->done);
  186. pr_debug("%s: cmd='%s' done\n", __func__, cmdstr(cmd->cmd));
  187. }
  188. static struct gelic_eurus_cmd *gelic_eurus_sync_cmd(struct gelic_wl_info *wl,
  189. unsigned int eurus_cmd,
  190. void *buffer,
  191. unsigned int buf_size)
  192. {
  193. struct gelic_eurus_cmd *cmd;
  194. /* allocate cmd */
  195. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  196. if (!cmd)
  197. return NULL;
  198. /* initialize members */
  199. cmd->cmd = eurus_cmd;
  200. cmd->buffer = buffer;
  201. cmd->buf_size = buf_size;
  202. cmd->wl = wl;
  203. INIT_WORK(&cmd->work, gelic_eurus_sync_cmd_worker);
  204. init_completion(&cmd->done);
  205. queue_work(wl->eurus_cmd_queue, &cmd->work);
  206. /* wait for command completion */
  207. wait_for_completion(&cmd->done);
  208. return cmd;
  209. }
  210. static u32 gelic_wl_get_link(struct net_device *netdev)
  211. {
  212. struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
  213. u32 ret;
  214. pr_debug("%s: <-\n", __func__);
  215. 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. for (i = 0, scan_info_size = 0, scan_info = wl->buf;
  1464. scan_info_size < data_len;
  1465. i++, scan_info_size += be16_to_cpu(scan_info->size),
  1466. scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
  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. /*
  1471. * The wireless firmware may return invalid channel 0 and/or
  1472. * invalid rate if the AP emits zero length SSID ie. As this
  1473. * scan information is useless, ignore it
  1474. */
  1475. if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
  1476. pr_debug("%s: invalid scan info\n", __func__);
  1477. continue;
  1478. }
  1479. found = 0;
  1480. oldest = NULL;
  1481. list_for_each_entry(target, &wl->network_list, list) {
  1482. if (!compare_ether_addr(&target->hwinfo->bssid[2],
  1483. &scan_info->bssid[2])) {
  1484. found = 1;
  1485. pr_debug("%s: same BBS found scanned list\n",
  1486. __func__);
  1487. break;
  1488. }
  1489. if (!oldest ||
  1490. (target->last_scanned < oldest->last_scanned))
  1491. oldest = target;
  1492. }
  1493. if (!found) {
  1494. /* not found in the list */
  1495. if (list_empty(&wl->network_free_list)) {
  1496. /* expire oldest */
  1497. target = oldest;
  1498. } else {
  1499. target = list_entry(wl->network_free_list.next,
  1500. struct gelic_wl_scan_info,
  1501. list);
  1502. }
  1503. }
  1504. /* update the item */
  1505. target->last_scanned = this_time;
  1506. target->valid = 1;
  1507. target->eurus_index = i;
  1508. kfree(target->hwinfo);
  1509. target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
  1510. GFP_KERNEL);
  1511. if (!target->hwinfo) {
  1512. pr_info("%s: kzalloc failed\n", __func__);
  1513. continue;
  1514. }
  1515. /* copy hw scan info */
  1516. memcpy(target->hwinfo, scan_info, scan_info->size);
  1517. target->essid_len = strnlen(scan_info->essid,
  1518. sizeof(scan_info->essid));
  1519. target->rate_len = 0;
  1520. for (r = 0; r < MAX_RATES_LENGTH; r++)
  1521. if (scan_info->rate[r])
  1522. target->rate_len++;
  1523. if (8 < target->rate_len)
  1524. pr_info("%s: AP returns %d rates\n", __func__,
  1525. target->rate_len);
  1526. target->rate_ext_len = 0;
  1527. for (r = 0; r < MAX_RATES_EX_LENGTH; r++)
  1528. if (scan_info->ext_rate[r])
  1529. target->rate_ext_len++;
  1530. list_move_tail(&target->list, &wl->network_list);
  1531. }
  1532. memset(&data, 0, sizeof(data));
  1533. wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
  1534. NULL);
  1535. out:
  1536. complete(&wl->scan_done);
  1537. up(&wl->scan_lock);
  1538. pr_debug("%s:end\n", __func__);
  1539. }
  1540. /*
  1541. * Select an appropriate bss from current scan list regarding
  1542. * current settings from userspace.
  1543. * The caller must hold wl->scan_lock,
  1544. * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
  1545. */
  1546. static void update_best(struct gelic_wl_scan_info **best,
  1547. struct gelic_wl_scan_info *candid,
  1548. int *best_weight,
  1549. int *weight)
  1550. {
  1551. if (*best_weight < ++(*weight)) {
  1552. *best_weight = *weight;
  1553. *best = candid;
  1554. }
  1555. }
  1556. static
  1557. struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
  1558. {
  1559. struct gelic_wl_scan_info *scan_info;
  1560. struct gelic_wl_scan_info *best_bss;
  1561. int weight, best_weight;
  1562. u16 security;
  1563. DECLARE_MAC_BUF(mac);
  1564. pr_debug("%s: <-\n", __func__);
  1565. best_bss = NULL;
  1566. best_weight = 0;
  1567. list_for_each_entry(scan_info, &wl->network_list, list) {
  1568. pr_debug("%s: station %p\n", __func__, scan_info);
  1569. if (!scan_info->valid) {
  1570. pr_debug("%s: station invalid\n", __func__);
  1571. continue;
  1572. }
  1573. /* If bss specified, check it only */
  1574. if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
  1575. if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
  1576. wl->bssid)) {
  1577. best_bss = scan_info;
  1578. pr_debug("%s: bssid matched\n", __func__);
  1579. break;
  1580. } else {
  1581. pr_debug("%s: bssid unmached\n", __func__);
  1582. continue;
  1583. }
  1584. }
  1585. weight = 0;
  1586. /* security */
  1587. security = be16_to_cpu(scan_info->hwinfo->security) &
  1588. GELIC_EURUS_SCAN_SEC_MASK;
  1589. if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
  1590. if (security == GELIC_EURUS_SCAN_SEC_WPA2)
  1591. update_best(&best_bss, scan_info,
  1592. &best_weight, &weight);
  1593. else
  1594. continue;
  1595. } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
  1596. if (security == GELIC_EURUS_SCAN_SEC_WPA)
  1597. update_best(&best_bss, scan_info,
  1598. &best_weight, &weight);
  1599. else
  1600. continue;
  1601. } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
  1602. wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  1603. if (security == GELIC_EURUS_SCAN_SEC_WEP)
  1604. update_best(&best_bss, scan_info,
  1605. &best_weight, &weight);
  1606. else
  1607. continue;
  1608. }
  1609. /* If ESSID is set, check it */
  1610. if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
  1611. if ((scan_info->essid_len == wl->essid_len) &&
  1612. !strncmp(wl->essid,
  1613. scan_info->hwinfo->essid,
  1614. scan_info->essid_len))
  1615. update_best(&best_bss, scan_info,
  1616. &best_weight, &weight);
  1617. else
  1618. continue;
  1619. }
  1620. }
  1621. #ifdef DEBUG
  1622. pr_debug("%s: -> bss=%p\n", __func__, best_bss);
  1623. if (best_bss) {
  1624. pr_debug("%s:addr=%s\n", __func__,
  1625. print_mac(mac, &best_bss->hwinfo->bssid[2]));
  1626. }
  1627. #endif
  1628. return best_bss;
  1629. }
  1630. /*
  1631. * Setup WEP configuration to the chip
  1632. * The caller must hold wl->scan_lock,
  1633. * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
  1634. */
  1635. static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
  1636. {
  1637. unsigned int i;
  1638. struct gelic_eurus_wep_cfg *wep;
  1639. struct gelic_eurus_cmd *cmd;
  1640. int wep104 = 0;
  1641. int have_key = 0;
  1642. int ret = 0;
  1643. pr_debug("%s: <-\n", __func__);
  1644. /* we can assume no one should uses the buffer */
  1645. wep = wl->buf;
  1646. memset(wep, 0, sizeof(*wep));
  1647. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  1648. pr_debug("%s: WEP mode\n", __func__);
  1649. for (i = 0; i < GELIC_WEP_KEYS; i++) {
  1650. if (!test_bit(i, &wl->key_enabled))
  1651. continue;
  1652. pr_debug("%s: key#%d enabled\n", __func__, i);
  1653. have_key = 1;
  1654. if (wl->key_len[i] == 13)
  1655. wep104 = 1;
  1656. else if (wl->key_len[i] != 5) {
  1657. pr_info("%s: wrong wep key[%d]=%d\n",
  1658. __func__, i, wl->key_len[i]);
  1659. ret = -EINVAL;
  1660. goto out;
  1661. }
  1662. memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
  1663. }
  1664. if (!have_key) {
  1665. pr_info("%s: all wep key disabled\n", __func__);
  1666. ret = -EINVAL;
  1667. goto out;
  1668. }
  1669. if (wep104) {
  1670. pr_debug("%s: 104bit key\n", __func__);
  1671. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
  1672. } else {
  1673. pr_debug("%s: 40bit key\n", __func__);
  1674. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
  1675. }
  1676. } else {
  1677. pr_debug("%s: NO encryption\n", __func__);
  1678. wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
  1679. }
  1680. /* issue wep setup */
  1681. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
  1682. wep, sizeof(*wep));
  1683. if (!cmd)
  1684. ret = -ENOMEM;
  1685. else if (cmd->status || cmd->cmd_status)
  1686. ret = -ENXIO;
  1687. kfree(cmd);
  1688. out:
  1689. pr_debug("%s: ->\n", __func__);
  1690. return ret;
  1691. }
  1692. #ifdef DEBUG
  1693. static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
  1694. {
  1695. switch (sec) {
  1696. case GELIC_EURUS_WPA_SEC_NONE:
  1697. return "NONE";
  1698. break;
  1699. case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
  1700. return "WPA_TKIP_TKIP";
  1701. break;
  1702. case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
  1703. return "WPA_TKIP_AES";
  1704. break;
  1705. case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
  1706. return "WPA_AES_AES";
  1707. break;
  1708. case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
  1709. return "WPA2_TKIP_TKIP";
  1710. break;
  1711. case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
  1712. return "WPA2_TKIP_AES";
  1713. break;
  1714. case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
  1715. return "WPA2_AES_AES";
  1716. break;
  1717. }
  1718. return "";
  1719. };
  1720. #endif
  1721. static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
  1722. {
  1723. struct gelic_eurus_wpa_cfg *wpa;
  1724. struct gelic_eurus_cmd *cmd;
  1725. u16 security;
  1726. int ret = 0;
  1727. pr_debug("%s: <-\n", __func__);
  1728. /* we can assume no one should uses the buffer */
  1729. wpa = wl->buf;
  1730. memset(wpa, 0, sizeof(*wpa));
  1731. if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
  1732. pr_info("%s: PSK not configured yet\n", __func__);
  1733. /* copy key */
  1734. memcpy(wpa->psk, wl->psk, wl->psk_len);
  1735. /* set security level */
  1736. if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
  1737. if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
  1738. security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
  1739. } else {
  1740. if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
  1741. precise_ie())
  1742. security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
  1743. else
  1744. security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
  1745. }
  1746. } else {
  1747. if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
  1748. security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
  1749. } else {
  1750. if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
  1751. precise_ie())
  1752. security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
  1753. else
  1754. security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
  1755. }
  1756. }
  1757. wpa->security = cpu_to_be16(security);
  1758. /* PSK type */
  1759. wpa->psk_type = cpu_to_be16(wl->psk_type);
  1760. #ifdef DEBUG
  1761. pr_debug("%s: sec=%s psktype=%s\nn", __func__,
  1762. wpasecstr(wpa->security),
  1763. (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
  1764. "BIN" : "passphrase");
  1765. #if 0
  1766. /*
  1767. * don't enable here if you plan to submit
  1768. * the debug log because this dumps your precious
  1769. * passphrase/key.
  1770. */
  1771. pr_debug("%s: psk=%s\n",
  1772. (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
  1773. (char *)"N/A" : (char *)wpa->psk);
  1774. #endif
  1775. #endif
  1776. /* issue wpa setup */
  1777. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
  1778. wpa, sizeof(*wpa));
  1779. if (!cmd)
  1780. ret = -ENOMEM;
  1781. else if (cmd->status || cmd->cmd_status)
  1782. ret = -ENXIO;
  1783. kfree(cmd);
  1784. pr_debug("%s: --> %d\n", __func__, ret);
  1785. return ret;
  1786. }
  1787. /*
  1788. * Start association. caller must hold assoc_stat_lock
  1789. */
  1790. static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
  1791. struct gelic_wl_scan_info *bss)
  1792. {
  1793. struct gelic_eurus_cmd *cmd;
  1794. struct gelic_eurus_common_cfg *common;
  1795. int ret = 0;
  1796. unsigned long rc;
  1797. pr_debug("%s: <-\n", __func__);
  1798. /* do common config */
  1799. common = wl->buf;
  1800. memset(common, 0, sizeof(*common));
  1801. common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
  1802. common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
  1803. common->scan_index = cpu_to_be16(bss->eurus_index);
  1804. switch (wl->auth_method) {
  1805. case GELIC_EURUS_AUTH_OPEN:
  1806. common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
  1807. break;
  1808. case GELIC_EURUS_AUTH_SHARED:
  1809. common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
  1810. break;
  1811. }
  1812. #ifdef DEBUG
  1813. scan_list_dump(wl);
  1814. #endif
  1815. pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
  1816. be16_to_cpu(common->scan_index),
  1817. be16_to_cpu(common->bss_type),
  1818. be16_to_cpu(common->auth_method));
  1819. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
  1820. common, sizeof(*common));
  1821. if (!cmd || cmd->status || cmd->cmd_status) {
  1822. ret = -ENOMEM;
  1823. kfree(cmd);
  1824. goto out;
  1825. }
  1826. kfree(cmd);
  1827. /* WEP/WPA */
  1828. switch (wl->wpa_level) {
  1829. case GELIC_WL_WPA_LEVEL_NONE:
  1830. /* If WEP or no security, setup WEP config */
  1831. ret = gelic_wl_do_wep_setup(wl);
  1832. break;
  1833. case GELIC_WL_WPA_LEVEL_WPA:
  1834. case GELIC_WL_WPA_LEVEL_WPA2:
  1835. ret = gelic_wl_do_wpa_setup(wl);
  1836. break;
  1837. };
  1838. if (ret) {
  1839. pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
  1840. ret);
  1841. }
  1842. /* start association */
  1843. init_completion(&wl->assoc_done);
  1844. wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
  1845. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
  1846. NULL, 0);
  1847. if (!cmd || cmd->status || cmd->cmd_status) {
  1848. pr_debug("%s: assoc request failed\n", __func__);
  1849. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1850. kfree(cmd);
  1851. ret = -ENOMEM;
  1852. gelic_wl_send_iwap_event(wl, NULL);
  1853. goto out;
  1854. }
  1855. kfree(cmd);
  1856. /* wait for connected event */
  1857. rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
  1858. if (!rc) {
  1859. /* timeouted. Maybe key or cyrpt mode is wrong */
  1860. pr_info("%s: connect timeout \n", __func__);
  1861. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
  1862. NULL, 0);
  1863. kfree(cmd);
  1864. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1865. gelic_wl_send_iwap_event(wl, NULL);
  1866. ret = -ENXIO;
  1867. } else {
  1868. wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
  1869. /* copy bssid */
  1870. memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
  1871. /* send connect event */
  1872. gelic_wl_send_iwap_event(wl, wl->active_bssid);
  1873. pr_info("%s: connected\n", __func__);
  1874. }
  1875. out:
  1876. pr_debug("%s: ->\n", __func__);
  1877. return ret;
  1878. }
  1879. /*
  1880. * connected event
  1881. */
  1882. static void gelic_wl_connected_event(struct gelic_wl_info *wl,
  1883. u64 event)
  1884. {
  1885. u64 desired_event = 0;
  1886. switch (wl->wpa_level) {
  1887. case GELIC_WL_WPA_LEVEL_NONE:
  1888. desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
  1889. break;
  1890. case GELIC_WL_WPA_LEVEL_WPA:
  1891. case GELIC_WL_WPA_LEVEL_WPA2:
  1892. desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
  1893. break;
  1894. }
  1895. if (desired_event == event) {
  1896. pr_debug("%s: completed \n", __func__);
  1897. complete(&wl->assoc_done);
  1898. netif_carrier_on(port_to_netdev(wl_port(wl)));
  1899. } else
  1900. pr_debug("%s: event %#lx under wpa\n",
  1901. __func__, event);
  1902. }
  1903. /*
  1904. * disconnect event
  1905. */
  1906. static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
  1907. u64 event)
  1908. {
  1909. struct gelic_eurus_cmd *cmd;
  1910. int lock;
  1911. /*
  1912. * If we fall here in the middle of association,
  1913. * associate_bss() should be waiting for complation of
  1914. * wl->assoc_done.
  1915. * As it waits with timeout, just leave assoc_done
  1916. * uncompleted, then it terminates with timeout
  1917. */
  1918. if (down_trylock(&wl->assoc_stat_lock)) {
  1919. pr_debug("%s: already locked\n", __func__);
  1920. lock = 0;
  1921. } else {
  1922. pr_debug("%s: obtain lock\n", __func__);
  1923. lock = 1;
  1924. }
  1925. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
  1926. kfree(cmd);
  1927. /* send disconnected event to the supplicant */
  1928. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  1929. gelic_wl_send_iwap_event(wl, NULL);
  1930. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  1931. netif_carrier_off(port_to_netdev(wl_port(wl)));
  1932. if (lock)
  1933. up(&wl->assoc_stat_lock);
  1934. }
  1935. /*
  1936. * event worker
  1937. */
  1938. #ifdef DEBUG
  1939. static const char *eventstr(enum gelic_lv1_wl_event event)
  1940. {
  1941. static char buf[32];
  1942. char *ret;
  1943. if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
  1944. ret = "EURUS_READY";
  1945. else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
  1946. ret = "SCAN_COMPLETED";
  1947. else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
  1948. ret = "DEAUTH";
  1949. else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
  1950. ret = "BEACON_LOST";
  1951. else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
  1952. ret = "CONNECTED";
  1953. else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
  1954. ret = "WPA_CONNECTED";
  1955. else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
  1956. ret = "WPA_ERROR";
  1957. else {
  1958. sprintf(buf, "Unknown(%#x)", event);
  1959. ret = buf;
  1960. }
  1961. return ret;
  1962. }
  1963. #else
  1964. static const char *eventstr(enum gelic_lv1_wl_event event)
  1965. {
  1966. return NULL;
  1967. }
  1968. #endif
  1969. static void gelic_wl_event_worker(struct work_struct *work)
  1970. {
  1971. struct gelic_wl_info *wl;
  1972. struct gelic_port *port;
  1973. u64 event, tmp;
  1974. int status;
  1975. pr_debug("%s:start\n", __func__);
  1976. wl = container_of(work, struct gelic_wl_info, event_work.work);
  1977. port = wl_port(wl);
  1978. while (1) {
  1979. status = lv1_net_control(bus_id(port->card), dev_id(port->card),
  1980. GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
  1981. &event, &tmp);
  1982. if (status) {
  1983. if (status != LV1_NO_ENTRY)
  1984. pr_debug("%s:wlan event failed %d\n",
  1985. __func__, status);
  1986. /* got all events */
  1987. pr_debug("%s:end\n", __func__);
  1988. return;
  1989. }
  1990. pr_debug("%s: event=%s\n", __func__, eventstr(event));
  1991. switch (event) {
  1992. case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
  1993. gelic_wl_scan_complete_event(wl);
  1994. break;
  1995. case GELIC_LV1_WL_EVENT_BEACON_LOST:
  1996. case GELIC_LV1_WL_EVENT_DEAUTH:
  1997. gelic_wl_disconnect_event(wl, event);
  1998. break;
  1999. case GELIC_LV1_WL_EVENT_CONNECTED:
  2000. case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
  2001. gelic_wl_connected_event(wl, event);
  2002. break;
  2003. default:
  2004. break;
  2005. }
  2006. } /* while */
  2007. }
  2008. /*
  2009. * association worker
  2010. */
  2011. static void gelic_wl_assoc_worker(struct work_struct *work)
  2012. {
  2013. struct gelic_wl_info *wl;
  2014. struct gelic_wl_scan_info *best_bss;
  2015. int ret;
  2016. wl = container_of(work, struct gelic_wl_info, assoc_work.work);
  2017. down(&wl->assoc_stat_lock);
  2018. if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
  2019. goto out;
  2020. ret = gelic_wl_start_scan(wl, 0);
  2021. if (ret == -ERESTARTSYS) {
  2022. pr_debug("%s: scan start failed association\n", __func__);
  2023. schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
  2024. goto out;
  2025. } else if (ret) {
  2026. pr_info("%s: scan prerequisite failed\n", __func__);
  2027. goto out;
  2028. }
  2029. /*
  2030. * Wait for bss scan completion
  2031. * If we have scan list already, gelic_wl_start_scan()
  2032. * returns OK and raises the complete. Thus,
  2033. * it's ok to wait unconditionally here
  2034. */
  2035. wait_for_completion(&wl->scan_done);
  2036. pr_debug("%s: scan done\n", __func__);
  2037. down(&wl->scan_lock);
  2038. if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
  2039. gelic_wl_send_iwap_event(wl, NULL);
  2040. pr_info("%s: no scan list. association failed\n", __func__);
  2041. goto scan_lock_out;
  2042. }
  2043. /* find best matching bss */
  2044. best_bss = gelic_wl_find_best_bss(wl);
  2045. if (!best_bss) {
  2046. gelic_wl_send_iwap_event(wl, NULL);
  2047. pr_info("%s: no bss matched. association failed\n", __func__);
  2048. goto scan_lock_out;
  2049. }
  2050. /* ok, do association */
  2051. ret = gelic_wl_associate_bss(wl, best_bss);
  2052. if (ret)
  2053. pr_info("%s: association failed %d\n", __func__, ret);
  2054. scan_lock_out:
  2055. up(&wl->scan_lock);
  2056. out:
  2057. up(&wl->assoc_stat_lock);
  2058. }
  2059. /*
  2060. * Interrupt handler
  2061. * Called from the ethernet interrupt handler
  2062. * Processes wireless specific virtual interrupts only
  2063. */
  2064. void gelic_wl_interrupt(struct net_device *netdev, u64 status)
  2065. {
  2066. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  2067. if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
  2068. pr_debug("%s:cmd complete\n", __func__);
  2069. complete(&wl->cmd_done_intr);
  2070. }
  2071. if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
  2072. pr_debug("%s:event received\n", __func__);
  2073. queue_delayed_work(wl->event_queue, &wl->event_work, 0);
  2074. }
  2075. }
  2076. /*
  2077. * driver helpers
  2078. */
  2079. #define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
  2080. static const iw_handler gelic_wl_wext_handler[] =
  2081. {
  2082. IW_IOCTL(SIOCGIWNAME) = gelic_wl_get_name,
  2083. IW_IOCTL(SIOCGIWRANGE) = gelic_wl_get_range,
  2084. IW_IOCTL(SIOCSIWSCAN) = gelic_wl_set_scan,
  2085. IW_IOCTL(SIOCGIWSCAN) = gelic_wl_get_scan,
  2086. IW_IOCTL(SIOCSIWAUTH) = gelic_wl_set_auth,
  2087. IW_IOCTL(SIOCGIWAUTH) = gelic_wl_get_auth,
  2088. IW_IOCTL(SIOCSIWESSID) = gelic_wl_set_essid,
  2089. IW_IOCTL(SIOCGIWESSID) = gelic_wl_get_essid,
  2090. IW_IOCTL(SIOCSIWENCODE) = gelic_wl_set_encode,
  2091. IW_IOCTL(SIOCGIWENCODE) = gelic_wl_get_encode,
  2092. IW_IOCTL(SIOCSIWAP) = gelic_wl_set_ap,
  2093. IW_IOCTL(SIOCGIWAP) = gelic_wl_get_ap,
  2094. IW_IOCTL(SIOCSIWENCODEEXT) = gelic_wl_set_encodeext,
  2095. IW_IOCTL(SIOCGIWENCODEEXT) = gelic_wl_get_encodeext,
  2096. IW_IOCTL(SIOCSIWMODE) = gelic_wl_set_mode,
  2097. IW_IOCTL(SIOCGIWMODE) = gelic_wl_get_mode,
  2098. IW_IOCTL(SIOCGIWNICKN) = gelic_wl_get_nick,
  2099. };
  2100. static struct iw_priv_args gelic_wl_private_args[] =
  2101. {
  2102. {
  2103. .cmd = GELIC_WL_PRIV_SET_PSK,
  2104. .set_args = IW_PRIV_TYPE_CHAR |
  2105. (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
  2106. .name = "set_psk"
  2107. },
  2108. {
  2109. .cmd = GELIC_WL_PRIV_GET_PSK,
  2110. .get_args = IW_PRIV_TYPE_CHAR |
  2111. (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
  2112. .name = "get_psk"
  2113. }
  2114. };
  2115. static const iw_handler gelic_wl_private_handler[] =
  2116. {
  2117. gelic_wl_priv_set_psk,
  2118. gelic_wl_priv_get_psk,
  2119. };
  2120. static const struct iw_handler_def gelic_wl_wext_handler_def = {
  2121. .num_standard = ARRAY_SIZE(gelic_wl_wext_handler),
  2122. .standard = gelic_wl_wext_handler,
  2123. .get_wireless_stats = gelic_wl_get_wireless_stats,
  2124. .num_private = ARRAY_SIZE(gelic_wl_private_handler),
  2125. .num_private_args = ARRAY_SIZE(gelic_wl_private_args),
  2126. .private = gelic_wl_private_handler,
  2127. .private_args = gelic_wl_private_args,
  2128. };
  2129. static struct net_device *gelic_wl_alloc(struct gelic_card *card)
  2130. {
  2131. struct net_device *netdev;
  2132. struct gelic_port *port;
  2133. struct gelic_wl_info *wl;
  2134. unsigned int i;
  2135. pr_debug("%s:start\n", __func__);
  2136. netdev = alloc_etherdev(sizeof(struct gelic_port) +
  2137. sizeof(struct gelic_wl_info));
  2138. pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
  2139. if (!netdev)
  2140. return NULL;
  2141. strcpy(netdev->name, "wlan%d");
  2142. port = netdev_priv(netdev);
  2143. port->netdev = netdev;
  2144. port->card = card;
  2145. port->type = GELIC_PORT_WIRELESS;
  2146. wl = port_wl(port);
  2147. pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
  2148. /* allocate scan list */
  2149. wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
  2150. GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
  2151. if (!wl->networks)
  2152. goto fail_bss;
  2153. wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
  2154. if (!wl->eurus_cmd_queue)
  2155. goto fail_cmd_workqueue;
  2156. wl->event_queue = create_singlethread_workqueue("gelic_event");
  2157. if (!wl->event_queue)
  2158. goto fail_event_workqueue;
  2159. INIT_LIST_HEAD(&wl->network_free_list);
  2160. INIT_LIST_HEAD(&wl->network_list);
  2161. for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
  2162. list_add_tail(&wl->networks[i].list,
  2163. &wl->network_free_list);
  2164. init_completion(&wl->cmd_done_intr);
  2165. INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
  2166. INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
  2167. init_MUTEX(&wl->scan_lock);
  2168. init_MUTEX(&wl->assoc_stat_lock);
  2169. init_completion(&wl->scan_done);
  2170. /* for the case that no scan request is issued and stop() is called */
  2171. complete(&wl->scan_done);
  2172. spin_lock_init(&wl->lock);
  2173. wl->scan_age = 5*HZ; /* FIXME */
  2174. /* buffer for receiving scanned list etc */
  2175. BUILD_BUG_ON(PAGE_SIZE <
  2176. sizeof(struct gelic_eurus_scan_info) *
  2177. GELIC_EURUS_MAX_SCAN);
  2178. wl->buf = (void *)get_zeroed_page(GFP_KERNEL);
  2179. if (!wl->buf) {
  2180. pr_info("%s:buffer allocation failed\n", __func__);
  2181. goto fail_getpage;
  2182. }
  2183. pr_debug("%s:end\n", __func__);
  2184. return netdev;
  2185. fail_getpage:
  2186. destroy_workqueue(wl->event_queue);
  2187. fail_event_workqueue:
  2188. destroy_workqueue(wl->eurus_cmd_queue);
  2189. fail_cmd_workqueue:
  2190. kfree(wl->networks);
  2191. fail_bss:
  2192. free_netdev(netdev);
  2193. pr_debug("%s:end error\n", __func__);
  2194. return NULL;
  2195. }
  2196. static void gelic_wl_free(struct gelic_wl_info *wl)
  2197. {
  2198. struct gelic_wl_scan_info *scan_info;
  2199. unsigned int i;
  2200. pr_debug("%s: <-\n", __func__);
  2201. pr_debug("%s: destroy queues\n", __func__);
  2202. destroy_workqueue(wl->eurus_cmd_queue);
  2203. destroy_workqueue(wl->event_queue);
  2204. scan_info = wl->networks;
  2205. for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
  2206. kfree(scan_info->hwinfo);
  2207. kfree(wl->networks);
  2208. free_netdev(port_to_netdev(wl_port(wl)));
  2209. pr_debug("%s: ->\n", __func__);
  2210. }
  2211. static int gelic_wl_try_associate(struct net_device *netdev)
  2212. {
  2213. struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
  2214. int ret = -1;
  2215. unsigned int i;
  2216. pr_debug("%s: <-\n", __func__);
  2217. /* check constraits for start association */
  2218. /* for no access restriction AP */
  2219. if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
  2220. if (test_bit(GELIC_WL_STAT_CONFIGURED,
  2221. &wl->stat))
  2222. goto do_associate;
  2223. else {
  2224. pr_debug("%s: no wep, not configured\n", __func__);
  2225. return ret;
  2226. }
  2227. }
  2228. /* for WEP, one of four keys should be set */
  2229. if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
  2230. /* one of keys set */
  2231. for (i = 0; i < GELIC_WEP_KEYS; i++) {
  2232. if (test_bit(i, &wl->key_enabled))
  2233. goto do_associate;
  2234. }
  2235. pr_debug("%s: WEP, but no key specified\n", __func__);
  2236. return ret;
  2237. }
  2238. /* for WPA[2], psk should be set */
  2239. if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
  2240. (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
  2241. if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
  2242. &wl->stat))
  2243. goto do_associate;
  2244. else {
  2245. pr_debug("%s: AES/TKIP, but PSK not configured\n",
  2246. __func__);
  2247. return ret;
  2248. }
  2249. }
  2250. do_associate:
  2251. ret = schedule_delayed_work(&wl->assoc_work, 0);
  2252. pr_debug("%s: start association work %d\n", __func__, ret);
  2253. return ret;
  2254. }
  2255. /*
  2256. * netdev handlers
  2257. */
  2258. static int gelic_wl_open(struct net_device *netdev)
  2259. {
  2260. struct gelic_card *card = netdev_card(netdev);
  2261. pr_debug("%s:->%p\n", __func__, netdev);
  2262. gelic_card_up(card);
  2263. /* try to associate */
  2264. gelic_wl_try_associate(netdev);
  2265. netif_start_queue(netdev);
  2266. pr_debug("%s:<-\n", __func__);
  2267. return 0;
  2268. }
  2269. /*
  2270. * reset state machine
  2271. */
  2272. static int gelic_wl_reset_state(struct gelic_wl_info *wl)
  2273. {
  2274. struct gelic_wl_scan_info *target;
  2275. struct gelic_wl_scan_info *tmp;
  2276. /* empty scan list */
  2277. list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
  2278. list_move_tail(&target->list, &wl->network_free_list);
  2279. }
  2280. wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
  2281. /* clear configuration */
  2282. wl->auth_method = GELIC_EURUS_AUTH_OPEN;
  2283. wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
  2284. wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
  2285. wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
  2286. wl->key_enabled = 0;
  2287. wl->current_key = 0;
  2288. wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
  2289. wl->psk_len = 0;
  2290. wl->essid_len = 0;
  2291. memset(wl->essid, 0, sizeof(wl->essid));
  2292. memset(wl->bssid, 0, sizeof(wl->bssid));
  2293. memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
  2294. wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
  2295. memset(&wl->iwstat, 0, sizeof(wl->iwstat));
  2296. /* all status bit clear */
  2297. wl->stat = 0;
  2298. return 0;
  2299. }
  2300. /*
  2301. * Tell eurus to terminate association
  2302. */
  2303. static void gelic_wl_disconnect(struct net_device *netdev)
  2304. {
  2305. struct gelic_port *port = netdev_priv(netdev);
  2306. struct gelic_wl_info *wl = port_wl(port);
  2307. struct gelic_eurus_cmd *cmd;
  2308. /*
  2309. * If scann process is running on chip,
  2310. * further requests will be rejected
  2311. */
  2312. if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
  2313. wait_for_completion_timeout(&wl->scan_done, HZ);
  2314. cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
  2315. kfree(cmd);
  2316. gelic_wl_send_iwap_event(wl, NULL);
  2317. };
  2318. static int gelic_wl_stop(struct net_device *netdev)
  2319. {
  2320. struct gelic_port *port = netdev_priv(netdev);
  2321. struct gelic_wl_info *wl = port_wl(port);
  2322. struct gelic_card *card = netdev_card(netdev);
  2323. pr_debug("%s:<-\n", __func__);
  2324. /*
  2325. * Cancel pending association work.
  2326. * event work can run after netdev down
  2327. */
  2328. cancel_delayed_work(&wl->assoc_work);
  2329. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  2330. gelic_wl_disconnect(netdev);
  2331. /* reset our state machine */
  2332. gelic_wl_reset_state(wl);
  2333. netif_stop_queue(netdev);
  2334. gelic_card_down(card);
  2335. pr_debug("%s:->\n", __func__);
  2336. return 0;
  2337. }
  2338. /* -- */
  2339. static struct ethtool_ops gelic_wl_ethtool_ops = {
  2340. .get_drvinfo = gelic_net_get_drvinfo,
  2341. .get_link = gelic_wl_get_link,
  2342. .get_tx_csum = ethtool_op_get_tx_csum,
  2343. .set_tx_csum = ethtool_op_set_tx_csum,
  2344. .get_rx_csum = gelic_net_get_rx_csum,
  2345. .set_rx_csum = gelic_net_set_rx_csum,
  2346. };
  2347. static void gelic_wl_setup_netdev_ops(struct net_device *netdev)
  2348. {
  2349. struct gelic_wl_info *wl;
  2350. wl = port_wl(netdev_priv(netdev));
  2351. BUG_ON(!wl);
  2352. netdev->open = &gelic_wl_open;
  2353. netdev->stop = &gelic_wl_stop;
  2354. netdev->hard_start_xmit = &gelic_net_xmit;
  2355. netdev->set_multicast_list = &gelic_net_set_multi;
  2356. netdev->change_mtu = &gelic_net_change_mtu;
  2357. netdev->wireless_data = &wl->wireless_data;
  2358. netdev->wireless_handlers = &gelic_wl_wext_handler_def;
  2359. /* tx watchdog */
  2360. netdev->tx_timeout = &gelic_net_tx_timeout;
  2361. netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
  2362. netdev->ethtool_ops = &gelic_wl_ethtool_ops;
  2363. #ifdef CONFIG_NET_POLL_CONTROLLER
  2364. netdev->poll_controller = gelic_net_poll_controller;
  2365. #endif
  2366. }
  2367. /*
  2368. * driver probe/remove
  2369. */
  2370. int gelic_wl_driver_probe(struct gelic_card *card)
  2371. {
  2372. int ret;
  2373. struct net_device *netdev;
  2374. pr_debug("%s:start\n", __func__);
  2375. if (ps3_compare_firmware_version(1, 6, 0) < 0)
  2376. return 0;
  2377. if (!card->vlan[GELIC_PORT_WIRELESS].tx)
  2378. return 0;
  2379. /* alloc netdevice for wireless */
  2380. netdev = gelic_wl_alloc(card);
  2381. if (!netdev)
  2382. return -ENOMEM;
  2383. /* setup net_device structure */
  2384. SET_NETDEV_DEV(netdev, &card->dev->core);
  2385. gelic_wl_setup_netdev_ops(netdev);
  2386. /* setup some of net_device and register it */
  2387. ret = gelic_net_setup_netdev(netdev, card);
  2388. if (ret)
  2389. goto fail_setup;
  2390. card->netdev[GELIC_PORT_WIRELESS] = netdev;
  2391. /* add enable wireless interrupt */
  2392. card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
  2393. GELIC_CARD_WLAN_COMMAND_COMPLETED;
  2394. /* to allow wireless commands while both interfaces are down */
  2395. gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
  2396. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  2397. pr_debug("%s:end\n", __func__);
  2398. return 0;
  2399. fail_setup:
  2400. gelic_wl_free(port_wl(netdev_port(netdev)));
  2401. return ret;
  2402. }
  2403. int gelic_wl_driver_remove(struct gelic_card *card)
  2404. {
  2405. struct gelic_wl_info *wl;
  2406. struct net_device *netdev;
  2407. pr_debug("%s:start\n", __func__);
  2408. if (ps3_compare_firmware_version(1, 6, 0) < 0)
  2409. return 0;
  2410. if (!card->vlan[GELIC_PORT_WIRELESS].tx)
  2411. return 0;
  2412. netdev = card->netdev[GELIC_PORT_WIRELESS];
  2413. wl = port_wl(netdev_priv(netdev));
  2414. /* if the interface was not up, but associated */
  2415. if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
  2416. gelic_wl_disconnect(netdev);
  2417. complete(&wl->cmd_done_intr);
  2418. /* cancel all work queue */
  2419. cancel_delayed_work(&wl->assoc_work);
  2420. cancel_delayed_work(&wl->event_work);
  2421. flush_workqueue(wl->eurus_cmd_queue);
  2422. flush_workqueue(wl->event_queue);
  2423. unregister_netdev(netdev);
  2424. /* disable wireless interrupt */
  2425. pr_debug("%s: disable intr\n", __func__);
  2426. card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
  2427. GELIC_CARD_WLAN_COMMAND_COMPLETED);
  2428. /* free bss list, netdev*/
  2429. gelic_wl_free(wl);
  2430. pr_debug("%s:end\n", __func__);
  2431. return 0;
  2432. }