ps3_gelic_wireless.c 69 KB

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