ps3_gelic_wireless.c 68 KB

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