ps3_gelic_wireless.c 69 KB

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