ps3_gelic_wireless.c 68 KB

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