ps3_gelic_wireless.c 68 KB

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