ps3_gelic_wireless.c 70 KB

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